/*******************************************************************************
 * Project: iir
 * Package: com.hngtrust.iir.elk.service.impl
 * Type:    ElkServiceImpl
 * Author:  Zhangkangkang
 * Date:    2021-04-14 10:00:00
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *******************************************************************************/
package com.hngtrust.iir.elk.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.framework.util.JsonUtils;
import com.hngtrust.iir.base.controller.vo.OptLogReqVO;
import com.hngtrust.iir.base.entity.Dict;
import com.hngtrust.iir.base.entity.IndustrySw;
import com.hngtrust.iir.base.service.DictService;
import com.hngtrust.iir.base.service.IndustrySwService;
import com.hngtrust.iir.base.service.LogService;
import com.hngtrust.iir.base.service.ReportTypeService;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.elk.dto.*;
import com.hngtrust.iir.elk.exception.ElkErrorType;
import com.hngtrust.iir.elk.exception.ElkException;
import com.hngtrust.iir.elk.service.ElkService;
import com.hngtrust.iir.material.controller.dto.SearchPrivateMaterialVO;
import com.hngtrust.iir.material.entity.MaterialCollect;
import com.hngtrust.iir.material.service.MaterialCollectService;
import com.hngtrust.iir.material.service.MaterialService;
import com.hngtrust.iir.report.controller.dto.SearchPrivateReportVO;
import com.hngtrust.iir.report.entity.PrivateReport;
import com.hngtrust.iir.report.entity.ReportCollect;
import com.hngtrust.iir.report.service.ReportCollectService;
import com.hngtrust.iir.report.service.ReportService;
import com.hngtrust.iir.user.entity.CollectInfo;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.user.service.CollectInfoService;
import com.hngtrust.iir.user.service.DepartmentService;
import com.hngtrust.iir.user.service.UserService;
import com.hngtrust.iir.util.ElkUtils;
import com.tiger.util.MyHttpClient;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * (ElkService)服务实现类
 *
 * @author Zhangkangkang
 * @since 2021-04-14 10:00:00
 */
@Service("ElkService")
@Slf4j
public class ElkServiceImpl implements ElkService {

    //HTTP请求工具包
    private static final MyHttpClient MY_HTTP_CLIENT = new MyHttpClient();

    //处理http请求返回结果对象
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    //ELK的url地址
    @Value("${elk.search.url:}")
    private String elkUrl;
    
    //minio地址
    @Value("${tiger.minio.out.address}")
    private String minioAddress;
    
    //研报索引名称
    @Value("${elk.search.indexname.report:}")
    private String indexReportName;

    //公告索引名称
    @Value("${elk.search.indexname.notice:}")
    private String indexNoticeName;
    
    @Autowired
    private LogService logService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CollectInfoService collectInfoService;
    
    //新闻资讯索引名称
    @Value("${elk.search.indexname.news:}")
    private String indexNewsName;
    
    @Value("${elk.search.indexname.comp}")
    private String indexCompName;

    @Value("${elk.search.indexname.point}")
    private String indexPointName;

    @Value("${elk.search.indexname.means}")
    private String indexMeansName;
    
    @Value("${elk.search.indexname.private-report}")
    private String indexPrivateReport;
    
    @Override
    public List<NoticeResp> searchNotice(NoticeReq params) {
        //获取连接
        String httpUrl = getElkSearchUrl(indexNoticeName);

        //获取查询参数
        JSONObject queryParams = getNoticeParams(params);
        OptLogReqVO vo = new OptLogReqVO();
        StringBuilder type = new StringBuilder();
        params.getNoticeTypeTags().forEach(n->{
            type.append(n);
        });
        String beginStr = null;
        String endStr = null;
        if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_ONE.equals(params.getTimeType())) {
             beginStr = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(),-1), DatePattern.NORM_DATE_PATTERN);
             endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
            
        } else if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_THREE.equals(params.getTimeType())) {
             beginStr = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -3), DatePattern.NORM_DATE_PATTERN);
             endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
       
        } else if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_SIX.equals(params.getTimeType())) {
             beginStr = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -6), DatePattern.NORM_DATE_PATTERN);
             endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
            
        } else if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_YEAR.equals(params.getTimeType())){
             beginStr = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -12), DatePattern.NORM_DATE_PATTERN);
             endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
           
        } else if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_DEFINE.equals(params.getTimeType())) {
            if (null == params.getBeginDate() || null == params.getEndDate()) {
                throw new ElkException(ElkErrorType.ELK_SEARCH_DATE_NULL);
            }
        
             beginStr = DateUtil.format(params.getBeginDate(), DatePattern.NORM_DATE_PATTERN);
             endStr = DateUtil.format(DateUtil.offsetDay(params.getEndDate(), 1), DatePattern.NORM_DATE_PATTERN);
        }
        StringBuilder industry = new StringBuilder();
        params.getIndustryTags().forEach(i->{
            industry.append(i);
        });
        vo.setOptContent("搜索，公告类型：:"+type.toString()+"+行业分类:"+industry+"+筛选时间:"+beginStr+endStr+"+关键字:"+params.getContent());
        vo.setOptType(3);
        vo.setOptType(9);
        try {
            log.info("查询参数:{}",queryParams.toString());
            String content = MY_HTTP_CLIENT.postJson(httpUrl, queryParams);
            return this.handleNoticeResult(content,params);
        } catch (Exception e) {
            log.error("ELK检索公告索引地址:{}异常:{}", httpUrl, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_INDEX_EXCEPTION, httpUrl);
        }
    }

    @Override
    public List<ReportResp> searchReport(ReportReq params) {
        //获取连接
        String httpUrl = getElkSearchUrl(indexReportName);

        //获取研报查询参数
        JSONObject queryParams = getReportParams(params);
        OptLogReqVO vo = new OptLogReqVO();
        vo.setOptContent("输入"+params.getContent()+"检索研究报告");
        vo.setOptType(3);
        vo.setOptModule(4);
        String s = queryParams.toString();
        try {
            log.info("查询参数:{}",s);
            String content = MY_HTTP_CLIENT.postJson(httpUrl, queryParams);
            vo.setStatus(1);
            logService.saveOptLog(vo);
            return this.handleReportResult(content,params);
        } catch (Exception e) {
            log.error("ELK检索研报索引地址:{}异常:{}", httpUrl, e);
            vo.setStatus(2);
            logService.saveOptLog(vo);
            throw new ElkException(ElkErrorType.ELK_SEARCH_INDEX_EXCEPTION, httpUrl);
        }
    }

    @Override
    public List<NewsInfoResp> searchNewsInfo(NewsInfoReq params) {
        //获取连接
        String httpUrl = getElkSearchUrl(indexNewsName);

        //获取查询参数
        JSONObject queryParams = getNewsInfoParams(params);
        OptLogReqVO vo = new OptLogReqVO();
        vo.setOptContent("输入"+params.getContent()+"检索新闻资讯");
        vo.setOptType(3);
        vo.setOptModule(5);
        try {
            log.info("查询参数:{}",queryParams.toString());
            String content = MY_HTTP_CLIENT.postJson(httpUrl, queryParams);
            vo.setStatus(1);
            logService.saveOptLog(vo);
            return handleNewsInfoResult(content,params);
        } catch (Exception e) {
            vo.setStatus(2);
            logService.saveOptLog(vo);
            log.error("ELK检索新闻资讯索引地址:{}异常:{}", httpUrl, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_INDEX_EXCEPTION, httpUrl);
        }
    }
    
    @Override
    public List<CompResp> searchCompInfo(CompSearchReq req) {
        String elkSearchUrl = getElkSearchUrl(indexCompName);
        JSONObject compInfoParams = getCompInfoParams(req);
        log.info("elk公司检索参数"+compInfoParams.toString());
        OptLogReqVO vo = new OptLogReqVO();
        vo.setOptContent("输入"+req.getCompName()+"检索公司画像");
        vo.setOptType(3);
        vo.setOptModule(3);
        try {
            vo.setStatus(1);
            logService.saveOptLog(vo);
            String content = MY_HTTP_CLIENT.postJson(elkSearchUrl, compInfoParams);
            return handleCompInfoResult(content);
        } catch (Exception e) {
            log.error("ELK检索公司索引地址:{}异常:{}", elkSearchUrl, e);
            vo.setStatus(2);
            logService.saveOptLog(vo);
            throw new ElkException(ElkErrorType.ELK_SEARCH_INDEX_EXCEPTION, elkSearchUrl);
        }
    }
    
    @Override
    public void updateNoticeTags(String id, Set<String> tags) {
        if (StringUtil.isBlank(id)) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_TAGS_ID_NULL);
        }
        //查询原数据URL
        String queryUrl = this.getElkSearchByIdUrl(indexNoticeName, id);
        //原内容
        String content = null;
        try {
            content = MY_HTTP_CLIENT.get(queryUrl);
        } catch (Exception e) {
            log.error("获取公告原文档失败索引ID:{}异常信息:{}", id, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_GETDOC_ERROR);
        }
        

        //修改数据URL
        String updateUrl = this.getElkSearchUpdateUrl(indexNoticeName, id);

        //修改标签
        try {
            MY_HTTP_CLIENT.postJson(updateUrl, getUpdateParams(tags));
        } catch (Exception e) {
            log.error("修改ELK公告文档标签失败:{}", e);
            throw new ElkException(ElkErrorType.Elk_SEARCH_GETDOC_UPDATE_ERROR);
        }
    }

    @Override
    public void updateNewsInfoTags(String id, Set<String> tags) {
        if (StringUtil.isBlank(id)) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_TAGS_ID_NULL);
        }
        //查询原数据URL
        String queryUrl = this.getElkSearchByIdUrl(indexNewsName, id);
        //原内容
        String content = null;
        try {
            content = MY_HTTP_CLIENT.get(queryUrl);
        } catch (Exception e) {
            log.error("获取新闻资讯原文档失败索引ID:{}异常信息:{}", id, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_GETDOC_ERROR);
        }
        
        //修改数据URL
        String updateUrl = this.getElkSearchUpdateUrl(indexNewsName, id);
        //修改标签
        try {
            MY_HTTP_CLIENT.postJson(updateUrl, getUpdateParams(tags));
        } catch (Exception e) {
            log.error("修改ELK新闻资讯文档标签失败:{}", e);
            throw new ElkException(ElkErrorType.Elk_SEARCH_GETDOC_UPDATE_ERROR);
        }
    }

    @Override
    public void updateReportTags(String id, Set<String> tags) {
        if (StringUtil.isBlank(id)) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_TAGS_ID_NULL);
        }

        //查询原数据URL
        String queryUrl = this.getElkSearchByIdUrl(indexReportName, id);
        //原内容
        String content = null;
        try {
            content = MY_HTTP_CLIENT.get(queryUrl);
        } catch (Exception e) {
            log.error("获取研报原文档失败索引ID:{}异常信息:{}", id, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_GETDOC_ERROR);
        }
        
        //修改数据URL
        String updateUrl = this.getElkSearchUpdateUrl(indexReportName, id);

        //修改标签
        try {
            MY_HTTP_CLIENT.postJson(updateUrl, getUpdateParams(tags));
        } catch (Exception e) {
            log.error("修改ELK研报文档标签失败:{}", e);
            throw new ElkException(ElkErrorType.Elk_SEARCH_GETDOC_UPDATE_ERROR);
        }
    }

    //处理公告返回结果
    private List<NoticeResp> handleNoticeResult(String content,NoticeReq params) {
        if (null == content) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_NULL);
        }
        try {
            JsonNode readTree = OBJECT_MAPPER.readTree(content);
            List<Map<String, JsonNode>> listNode = getIirListObject(readTree);
            List<NoticeResp> listResult = new ArrayList<>();
            if (null == listNode || listNode.isEmpty()) {
                return listResult;
            }
            for (Map<String, JsonNode> mapNode : listNode) {
                NoticeResp entity = new NoticeResp();
                JsonNode iirNode = mapNode.get(ElkUtils.ELK_IIR);
                entity.setElkId(mapNode.get(ElkUtils.ELK_ID).asText());
                entity.setElkType(mapNode.get(ElkUtils.ELK_TYPE).asText());
                entity.setElkIndex(mapNode.get(ElkUtils.ELK_INDEX).asText());
                entity.setClickHouseId(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CLICKHOUSEID));
                entity.setCurrentTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_CURRENTTIME));
                entity.setTitle(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_TITLE));
                entity.setPublishTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_PUBLISHTIME));
                entity.setContent(null);        //内容前端列表不展示，为了减小大小设置为空
                String shortContent = this.getShortContent(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CONTENT),params.getContent());
                entity.setShortContent(shortContent);
                //获取标签
                Set<String> tags = new HashSet<>();
                JsonNode tagNode = iirNode.get(ElkUtils.ELK_IIR_TAGS);
                if (null != tagNode) {
                    for (JsonNode node : tagNode) {
                        if (null != node) {
                            tags.add(node.asText());
                        }
                    }
                }
                entity.setTags(tags);

                //获取自定义标签
                Set<String> selfTags = new HashSet<>();
                JsonNode selfTagNode = iirNode.get(ElkUtils.ELK_IIR_SELF_TAGS);
                if (null != selfTagNode) {
                    for (JsonNode node : selfTagNode) {
                        if (null != node) {
                            selfTags.add(node.asText());
                        }
                    }
                }
                entity.setSelfTags(selfTags);
                //获取排序码
                List<String> sorts = new ArrayList<>();
                JsonNode sortNode = mapNode.get(ElkUtils.ELK_SORT);
                if (null != sortNode) {
                    for (JsonNode node : sortNode) {
                        if (null != node) {
                            sorts.add(node.asText());
                        }
                    }
                }
                entity.setSort(sorts);
                
                //entity.setType(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_TYPE));
                entity.setIndustryId(ElkUtils.getLong(iirNode, ElkUtils.ELK_IIR_INDUSTRYID));
                entity.setIndustryName(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_INDUSTRYNAME));
                ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
                Set<String> compNames = new HashSet<>();
                int count=0;
                JsonNode compNameNode = iirNode.get(ElkUtils.ELK_IIR_COMPNAME);
                if (null != compNameNode) {
                    for (JsonNode node : compNameNode) {
                        if (null != node) {
                            if(++count<=10){
                                compNames.add(node.asText());
                            }else{
                                break;
                            }
                        }
                    }
                }
                entity.setCompNames(compNames);
                entity.setKeyWords(params.getContent());
                listResult.add(entity);
            }
            return listResult;
        } catch (Exception e) {
            log.error("ELK检索公告返回内容:{}解析异常:{}", content, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }
    }
    
    private List<CompResp> handleCompInfoResult(String content){
        if (null == content) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_NULL);
        }
        try{
            JsonNode readTree = OBJECT_MAPPER.readTree(content);
            List<Map<String, JsonNode>> iirListObject = getIirListObject(readTree);
            List<CompResp> result = new ArrayList<>();
            if (null == iirListObject || iirListObject.isEmpty()) {
                return result;
            }
            
            for (Map<String, JsonNode> json : iirListObject) {
                CompResp compResp = new CompResp();
                JsonNode iirNode = json.get(ElkUtils.ELK_IIR);
                compResp.setId(json.get(ElkUtils.ELK_ID).asText());
                compResp.setType(json.get(ElkUtils.ELK_TYPE).asText());
                compResp.setIndex(json.get(ElkUtils.ELK_INDEX).asText());
                compResp.setAddress(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_ADDRESS));
                compResp.setChairman(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_CHAIRMAN));
                compResp.setCompName(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_NAME));
                compResp.setManageStateName(ElkUtils.getString(iirNode,ElkUtils.ELK_IIR_COMP_MANGE_STATE_NAME));
                compResp.setCompId(Long.parseLong(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_ID)));
                if (null != ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_LOGO) && "" != ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_LOGO)){
                    compResp.setLogo(minioAddress + ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_LOGO));
                }
                if (null != ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_REG_CAPITAL) && "" != ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_REG_CAPITAL)){
                    compResp.setRegCapital(new BigDecimal(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_COMP_REG_CAPITAL)));
                }

                compResp.setFoundDate(ElkUtils.getDate(iirNode,ElkUtils.ELK_IIR_COMP_FOUND_DATE));
                compResp.setCreditCode(ElkUtils.getString(iirNode,ElkUtils.ELK_IIR_COMP_CREDIT_CODE));
                compResp.setBondCode(ElkUtils.getString(iirNode,ElkUtils.ELK_IIR_COMP_BOND_CODE));
                compResp.setCurrentTime(ElkUtils.getDate(iirNode,ElkUtils.ELK_IIR_CURRENTTIME));
                //获取排序码
                List<String> sorts = new ArrayList<>();
                JsonNode sortNode = json.get(ElkUtils.ELK_SORT);
                if (null != sortNode) {
                    for (JsonNode node : sortNode) {
                        if (null != node) {
                            sorts.add(node.asText());
                        }
                    }
                }
                compResp.setSort(sorts);
                result.add(compResp);
            }
            return result;
        }catch (Exception e) {
            log.error("ELK检索公司返回内容:{}解析异常:{}", content, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }
        
    }
    
    

    //处理新闻资讯返回结果
    private List<NewsInfoResp> handleNewsInfoResult(String content,NewsInfoReq params) {
        if (null == content) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_NULL);
        }

        try {
            JsonNode readTree = OBJECT_MAPPER.readTree(content);
            List<Map<String, JsonNode>> listNode = getIirListObject(readTree);

            List<NewsInfoResp> listResult = new ArrayList<>();
            if (null == listNode || listNode.isEmpty()) {
                return listResult;
            }
            for (Map<String, JsonNode> mapNode : listNode) {
                NewsInfoResp entity = new NewsInfoResp();
                JsonNode iirNode = mapNode.get(ElkUtils.ELK_IIR);
                entity.setElkId(mapNode.get(ElkUtils.ELK_ID).asText());
                entity.setElkType(mapNode.get(ElkUtils.ELK_TYPE).asText());
                entity.setElkIndex(mapNode.get(ElkUtils.ELK_INDEX).asText());
                entity.setClickHouseId(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CLICKHOUSEID));
                entity.setCurrentTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_CURRENTTIME));
                entity.setTitle(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_TITLE));
                entity.setPublishTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_PUBLISHTIME));
                entity.setContent(null);  //内容前端列表不展示，为了减小大小设置为空
                String shortContent = this.getShortContent(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CONTENT),params.getContent());
                entity.setShortContent(shortContent);
                entity.setIndustryId(ElkUtils.getLong(iirNode, ElkUtils.ELK_IIR_INDUSTRYID));
                entity.setIndustryName(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_INDUSTRYNAME));
                entity.setSource(ElkUtils.getString(iirNode,ElkUtils.ELK_IIR_SOURCE));
                Set<Map<String,Object>> compNames = new HashSet<>();
                JsonNode compNameNode = iirNode.get(ElkUtils.ELK_IIR_COMPNAME);
                int count=0;
                if (null != compNameNode) {
                    for (JsonNode node : compNameNode) {
                        if (null != node) {
                            if(++count<=10){
                                Map<String,Object> map = new HashMap<>();
                                map.put(ElkUtils.ELK_SUB_COMPNAME,ElkUtils.getString(node,ElkUtils.ELK_SUB_COMPNAME));
                                map.put(ElkUtils.ELK_SUB_SENTIMENT,ElkUtils.getString(node,ElkUtils.ELK_SUB_SENTIMENT));
                                compNames.add(map);
                            }else{
                                break;
                            }
                        }
                    }
                }
                entity.setCompNames(compNames);
                entity.setSentiment(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_SENTIMENT));
                Set<String> tags = new HashSet<>();
                //获取标签
                JsonNode tagNode = iirNode.get(ElkUtils.ELK_IIR_TAGS);
                if (null != tagNode) {
                    for (JsonNode node : tagNode) {
                        if (null != node) {
                            tags.add(node.asText());
                        }
                    }
                }
                entity.setTags(tags);

                //获取自定义标签
                Set<String> selfTags = new HashSet<>();
                JsonNode selfTagNode = iirNode.get(ElkUtils.ELK_IIR_SELF_TAGS);
                if (null != selfTagNode) {
                    for (JsonNode node : selfTagNode) {
                        if (null != node) {
                            selfTags.add(node.asText());
                        }
                    }
                }
                entity.setSelfTags(selfTags);

                //获取排序码
                List<String> sorts = new ArrayList<>();
                JsonNode sortNode = mapNode.get(ElkUtils.ELK_SORT);
                if (null != sortNode) {
                    for (JsonNode node : sortNode) {
                        if (null != node) {
                            sorts.add(node.asText());
                        }
                    }
                }
                entity.setSort(sorts);
                entity.setKeyWords(params.getContent());
                listResult.add(entity);
            }
            return listResult;
        } catch (Exception e) {
            log.error("ELK检索新闻资讯返回内容:{}解析异常:{}", content, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }
    }

    //处理行业资讯返回结果
    private List<IndustryNewsInfoResp> handleIndutryNewsInfoResult(String content,IndustryNewsInfoReq params) {
        if (null == content) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_NULL);
        }

        try {
            JsonNode readTree = OBJECT_MAPPER.readTree(content);
            List<Map<String, JsonNode>> listNode = getIirListObject(readTree);

            List<IndustryNewsInfoResp> listResult = new ArrayList<>();
            if (null == listNode || listNode.isEmpty()) {
                return listResult;
            }
            for (Map<String, JsonNode> mapNode : listNode) {
                IndustryNewsInfoResp entity = new IndustryNewsInfoResp();
                JsonNode iirNode = mapNode.get(ElkUtils.ELK_IIR);
                entity.setElkId(mapNode.get(ElkUtils.ELK_ID).asText());
                entity.setElkType(mapNode.get(ElkUtils.ELK_TYPE).asText());
                entity.setElkIndex(mapNode.get(ElkUtils.ELK_INDEX).asText());
                entity.setClickHouseId(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CLICKHOUSEID));
                entity.setCurrentTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_CURRENTTIME));
                entity.setTitle(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_TITLE));
                entity.setPublishTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_PUBLISHTIME));
                entity.setContent(null);  //内容前端列表不展示，为了减小大小设置为空
                String shortContent = this.getShortContent(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CONTENT),params.getContent());
                entity.setShortContent(shortContent);
                entity.setIndustryId(ElkUtils.getLong(iirNode, ElkUtils.ELK_IIR_INDUSTRYID));
                entity.setIndustryName(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_INDUSTRYNAME));
                entity.setSource(ElkUtils.getString(iirNode,ElkUtils.ELK_IIR_SOURCE));

                //是否已收藏
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                CollectInfo collect = new CollectInfo();
                collect.setInfoId(Long.getLong(entity.getClickHouseId()));
                collect.setCreateUserId(loginUser.getId());
                List<CollectInfo> collectList = collectInfoService.queryList(collect);
                if(!collectList.isEmpty()){
                    entity.setCollect(Constant.WHETHER_YES);
                }
                else{
                    entity.setCollect(Constant.WHETHER_NO);
                }

                Set<Map<String,Object>> compNames = new HashSet<>();
                JsonNode compNameNode = iirNode.get(ElkUtils.ELK_IIR_COMPNAME);
                int count=0;
                if (null != compNameNode) {
                    for (JsonNode node : compNameNode) {
                        if (null != node) {
                            if(++count<=10){
                                Map<String,Object> map = new HashMap<>();
                                map.put(ElkUtils.ELK_SUB_COMPNAME,ElkUtils.getString(node,ElkUtils.ELK_SUB_COMPNAME));
                                map.put(ElkUtils.ELK_SUB_SENTIMENT,ElkUtils.getString(node,ElkUtils.ELK_SUB_SENTIMENT));
                                compNames.add(map);
                            }else{
                                break;
                            }
                        }
                    }
                }
                entity.setCompNames(compNames);
                entity.setSentiment(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_SENTIMENT));
                Set<String> tags = new HashSet<>();
                //获取标签
                JsonNode tagNode = iirNode.get(ElkUtils.ELK_IIR_TAGS);
                if (null != tagNode) {
                    for (JsonNode node : tagNode) {
                        if (null != node) {
                            tags.add(node.asText());
                        }
                    }
                }
                entity.setTags(tags);

                //获取自定义标签
                Set<String> selfTags = new HashSet<>();
                JsonNode selfTagNode = iirNode.get(ElkUtils.ELK_IIR_SELF_TAGS);
                if (null != selfTagNode) {
                    for (JsonNode node : selfTagNode) {
                        if (null != node) {
                            selfTags.add(node.asText());
                        }
                    }
                }
                entity.setSelfTags(selfTags);

                //获取排序码
                List<String> sorts = new ArrayList<>();
                JsonNode sortNode = mapNode.get(ElkUtils.ELK_SORT);
                if (null != sortNode) {
                    for (JsonNode node : sortNode) {
                        if (null != node) {
                            sorts.add(node.asText());
                        }
                    }
                }
                entity.setSort(sorts);
                entity.setKeyWords(params.getContent());
                listResult.add(entity);
            }
            return listResult;
        } catch (Exception e) {
            log.error("ELK检索行业资讯返回内容:{}解析异常:{}", content, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }
    }

    //处理专家观点返回结果
    private List<IndustryPointsInfoResp> handleIndutryPointsInfoResult(String content,IndustryPointsInfoReq params) {
        if (null == content) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_NULL);
        }

        try {
            JsonNode readTree = OBJECT_MAPPER.readTree(content);
            List<Map<String, JsonNode>> listNode = getIirListObject(readTree);

            List<IndustryPointsInfoResp> listResult = new ArrayList<>();
            if (null == listNode || listNode.isEmpty()) {
                return listResult;
            }
            for (Map<String, JsonNode> mapNode : listNode) {
                IndustryPointsInfoResp entity = new IndustryPointsInfoResp();
                JsonNode iirNode = mapNode.get(ElkUtils.ELK_IIR);
                entity.setElkId(mapNode.get(ElkUtils.ELK_ID).asText());
                entity.setElkType(mapNode.get(ElkUtils.ELK_TYPE).asText());
                entity.setElkIndex(mapNode.get(ElkUtils.ELK_INDEX).asText());
                entity.setClickHouseId(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CLICKHOUSEID));
                entity.setCurrentTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_CURRENTTIME));
                entity.setTitle(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_TITLE));
                entity.setPublishTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_PUBLISHTIME));
                entity.setContent(null);  //内容前端列表不展示，为了减小大小设置为空
                String shortContent = this.getShortContent(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CONTENT),params.getContent());
                entity.setShortContent(shortContent);
                entity.setIndustryId(ElkUtils.getLong(iirNode, ElkUtils.ELK_IIR_INDUSTRYID));
                entity.setIndustryName(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_INDUSTRYNAME));
                entity.setSource(ElkUtils.getString(iirNode,ElkUtils.ELK_IIR_SOURCE));
                Set<Map<String,Object>> compNames = new HashSet<>();
                JsonNode compNameNode = iirNode.get(ElkUtils.ELK_IIR_COMPNAME);
                int count=0;
                if (null != compNameNode) {
                    for (JsonNode node : compNameNode) {
                        if (null != node) {
                            if(++count<=10){
                                Map<String,Object> map = new HashMap<>();
                                map.put(ElkUtils.ELK_SUB_COMPNAME,ElkUtils.getString(node,ElkUtils.ELK_SUB_COMPNAME));
                                map.put(ElkUtils.ELK_SUB_SENTIMENT,ElkUtils.getString(node,ElkUtils.ELK_SUB_SENTIMENT));
                                compNames.add(map);
                            }else{
                                break;
                            }
                        }
                    }
                }
                entity.setCompNames(compNames);
                //是否已收藏
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                CollectInfo collect = new CollectInfo();
                collect.setInfoId(Long.getLong(entity.getClickHouseId()));
                collect.setCreateUserId(loginUser.getId());
                List<CollectInfo> collectList = collectInfoService.queryList(collect);
                if(!collectList.isEmpty()){
                    entity.setCollect(Constant.WHETHER_YES);
                }
                else{
                    entity.setCollect(Constant.WHETHER_NO);
                }

                entity.setSentiment(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_SENTIMENT));
                Set<String> tags = new HashSet<>();
                //获取标签
                JsonNode tagNode = iirNode.get(ElkUtils.ELK_IIR_TAGS);
                if (null != tagNode) {
                    for (JsonNode node : tagNode) {
                        if (null != node) {
                            tags.add(node.asText());
                        }
                    }
                }
                entity.setTags(tags);

                //获取自定义标签
                Set<String> selfTags = new HashSet<>();
                JsonNode selfTagNode = iirNode.get(ElkUtils.ELK_IIR_SELF_TAGS);
                if (null != selfTagNode) {
                    for (JsonNode node : selfTagNode) {
                        if (null != node) {
                            selfTags.add(node.asText());
                        }
                    }
                }
                entity.setSelfTags(selfTags);

                //获取排序码
                List<String> sorts = new ArrayList<>();
                JsonNode sortNode = mapNode.get(ElkUtils.ELK_SORT);
                if (null != sortNode) {
                    for (JsonNode node : sortNode) {
                        if (null != node) {
                            sorts.add(node.asText());
                        }
                    }
                }
                entity.setSort(sorts);
                entity.setKeyWords(params.getContent());
                listResult.add(entity);
            }
            return listResult;
        } catch (Exception e) {
            log.error("ELK检索专家观点返回内容:{}解析异常:{}", content, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }
    }

    private List<Map<String, JsonNode>> getIirListObject(JsonNode readTree) {
        List<Map<String, JsonNode>> listNode = new ArrayList<>();
        //校验返回是否超时
        if (checkTimeOut(readTree)) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_TIME_OUT);
        }

        //获取父级hits内容
        JsonNode parentHitsNode = readTree.get(ElkUtils.ELK_HITS);
        if (null == parentHitsNode) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_BLANK, ElkUtils.ELK_HITS);
        }

        //获取hits内容
        JsonNode hitsNode = parentHitsNode.get(ElkUtils.ELK_HITS);
        if (null == hitsNode) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_BLANK, ElkUtils.ELK_HITS);
        }
        Iterator<JsonNode> arrayJsonNode = hitsNode.iterator();
        while (arrayJsonNode.hasNext()) {
            Map<String, JsonNode> data = new HashMap<>();
            JsonNode hitNode = arrayJsonNode.next();

            //获取ID
            JsonNode idNode = hitNode.get(ElkUtils.ELK_ID);
            if (null == idNode) {
                continue;
            }
            data.put(ElkUtils.ELK_ID, idNode);

            //获取文档类型
            JsonNode typeNode = hitNode.get(ElkUtils.ELK_TYPE);
            if (null == typeNode) {
                continue;
            }
            data.put(ElkUtils.ELK_TYPE, typeNode);

            //获取索引名称
            JsonNode indexNameNode = hitNode.get(ElkUtils.ELK_INDEX);
            if (null == indexNameNode) {
                continue;
            }
            data.put(ElkUtils.ELK_INDEX, indexNameNode);

            //获取_source
            JsonNode sourceNode = hitNode.get(ElkUtils.ELK_SOURCE);
            if (null == sourceNode) {
                continue;
            }

            //排序码sort
            JsonNode sortNode = hitNode.get(ElkUtils.ELK_SORT);
            if(null != sortNode){
                data.put(ElkUtils.ELK_SORT,sortNode);
            }
            //获取IIR
            JsonNode iirNode = sourceNode.get(ElkUtils.ELK_IIR);
            if (null == iirNode) {
                continue;
            }
            data.put(ElkUtils.ELK_IIR, iirNode);
            listNode.add(data);
        }
        return listNode;
    }

    //处理研报返回结果
    private List<ReportResp> handleReportResult(String content,ReportReq params) {
        if (StringUtil.isBlank(content)) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_NULL);
        }

        try {
            JsonNode readTree = OBJECT_MAPPER.readTree(content);
            List<Map<String, JsonNode>> listMapNode = getIirListObject(readTree);

            List<ReportResp> listResult = new ArrayList<>();
            if (listMapNode.isEmpty()) {
                return listResult;
            }
            for (Map<String, JsonNode> mapNode : listMapNode) {
                ReportResp entity = new ReportResp();
                JsonNode iirNode = mapNode.get(ElkUtils.ELK_IIR);
                entity.setElkId(mapNode.get(ElkUtils.ELK_ID).asText());
                entity.setElkType(mapNode.get(ElkUtils.ELK_TYPE).asText());
                entity.setElkIndex(mapNode.get(ElkUtils.ELK_INDEX).asText());
                entity.setClickHouseId(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CLICKHOUSEID));
                entity.setCurrentTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_CURRENTTIME));
                entity.setTitle(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_TITLE));
                entity.setPublishTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_PUBLISHTIME));
                entity.setContent(null);   //内容前端列表不展示，为了减小大小设置为空
                entity.setRankChange(ElkUtils.getString(iirNode,ElkUtils.ELK_IIR_RANKCHANGE));
                String shortContent = this.getShortContent(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CONTENT),params.getContent());
                entity.setShortContent(shortContent);
                Set<String> tags = new HashSet<>();
                //获取标签
                JsonNode tagNode = iirNode.get(ElkUtils.ELK_IIR_TAGS);
                if (null != tagNode) {
                    for (JsonNode node : tagNode) {
                        if (null != node) {
                            tags.add(node.asText());
                        }
                    }
                }
                entity.setTags(tags);

                //获取自定义标签
                Set<String> selfTags = new HashSet<>();
                JsonNode selfTagNode = iirNode.get(ElkUtils.ELK_IIR_SELF_TAGS);
                if (null != selfTagNode) {
                    for (JsonNode node : selfTagNode) {
                        if (null != node) {
                            selfTags.add(node.asText());
                        }
                    }
                }
                entity.setSelfTags(selfTags);

                //获取排序码
                List<String> sorts = new ArrayList<>();
                JsonNode sortNode = mapNode.get(ElkUtils.ELK_SORT);
                if (null != sortNode) {
                    for (JsonNode node : sortNode) {
                        if (null != node) {
                            sorts.add(node.asText());
                        }
                    }
                }
                entity.setSort(sorts);
                entity.setReportDate(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_REPORTDATE));
                entity.setAuther(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_AUTHER));
                entity.setType(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_TYPE));
                entity.setRank(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_RANK));
                entity.setViewpoint(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_VIEWPOINT));
                entity.setIndustryId(ElkUtils.getLong(iirNode, ElkUtils.ELK_IIR_INDUSTRYID));
                entity.setIndustryName(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_INDUSTRYNAME));
                entity.setPages(ElkUtils.getInt(iirNode,ElkUtils.ELK_IIR_PAGES));
                Set<String> compNames = new HashSet<>();
                JsonNode compNameNode = iirNode.get(ElkUtils.ELK_IIR_COMPNAME);
                int count=0;
                if (null != compNameNode) {
                    for (JsonNode node : compNameNode) {
                        if (null != node) {
                            if(++count<=10){
                                compNames.add(node.asText());
                            }else{
                                break;
                            }
                        }
                    }
                }
                entity.setCompNames(compNames);
                entity.setKeyWords(params.getContent());
                listResult.add(entity);
            }
            return listResult;
        } catch (Exception e) {
            log.error("ELK检索研报返回内容:{}解析异常:{}", content, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }
    }

    //处理行业研报返回结果
    private List<IndustryReportResp> handleIndustryReportResult(String content,IndustryReportReq params) {
        if (StringUtil.isBlank(content)) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_NULL);
        }

        try {
            JsonNode readTree = OBJECT_MAPPER.readTree(content);
            List<Map<String, JsonNode>> listMapNode = getIirListObject(readTree);

            List<IndustryReportResp> listResult = new ArrayList<>();
            if (listMapNode.isEmpty()) {
                return listResult;
            }
            for (Map<String, JsonNode> mapNode : listMapNode) {
                IndustryReportResp entity = new IndustryReportResp();
                JsonNode iirNode = mapNode.get(ElkUtils.ELK_IIR);
                entity.setElkId(mapNode.get(ElkUtils.ELK_ID).asText());
                entity.setElkType(mapNode.get(ElkUtils.ELK_TYPE).asText());
                entity.setElkIndex(mapNode.get(ElkUtils.ELK_INDEX).asText());
                entity.setClickHouseId(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CLICKHOUSEID));
                entity.setCurrentTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_CURRENTTIME));
                entity.setTitle(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_TITLE));
                entity.setPublishTime(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_PUBLISHTIME));
                entity.setContent(null);   //内容前端列表不展示，为了减小大小设置为空
                entity.setRankChange(ElkUtils.getString(iirNode,ElkUtils.ELK_IIR_RANKCHANGE));
                String shortContent = this.getShortContent(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_CONTENT),params.getContent());
                entity.setShortContent(shortContent);
                Set<String> tags = new HashSet<>();
                //获取标签
                JsonNode tagNode = iirNode.get(ElkUtils.ELK_IIR_TAGS);
                if (null != tagNode) {
                    for (JsonNode node : tagNode) {
                        if (null != node) {
                            tags.add(node.asText());
                        }
                    }
                }
                entity.setTags(tags);

                //获取自定义标签
                Set<String> selfTags = new HashSet<>();
                JsonNode selfTagNode = iirNode.get(ElkUtils.ELK_IIR_SELF_TAGS);
                if (null != selfTagNode) {
                    for (JsonNode node : selfTagNode) {
                        if (null != node) {
                            selfTags.add(node.asText());
                        }
                    }
                }
                entity.setSelfTags(selfTags);

                //获取排序码
                List<String> sorts = new ArrayList<>();
                JsonNode sortNode = mapNode.get(ElkUtils.ELK_SORT);
                if (null != sortNode) {
                    for (JsonNode node : sortNode) {
                        if (null != node) {
                            sorts.add(node.asText());
                        }
                    }
                }
                entity.setSort(sorts);
                //是否已收藏
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                CollectInfo collect = new CollectInfo();
                collect.setInfoId(Long.getLong(entity.getClickHouseId()));
                collect.setCreateUserId(loginUser.getId());
                List<CollectInfo> collectList = collectInfoService.queryList(collect);
                if(!collectList.isEmpty()){
                    entity.setCollect(Constant.WHETHER_YES);
                }
                else{
                    entity.setCollect(Constant.WHETHER_NO);
                }
                entity.setReportDate(ElkUtils.getDate(iirNode, ElkUtils.ELK_IIR_REPORTDATE));
                entity.setAuther(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_AUTHER));
                entity.setType(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_TYPE));
                entity.setRank(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_RANK));
                entity.setViewpoint(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_VIEWPOINT));
                entity.setIndustryId(ElkUtils.getLong(iirNode, ElkUtils.ELK_IIR_INDUSTRYID));
                entity.setIndustryName(ElkUtils.getString(iirNode, ElkUtils.ELK_IIR_INDUSTRYNAME));
                entity.setPages(ElkUtils.getInt(iirNode,ElkUtils.ELK_IIR_PAGES));
                Set<String> compNames = new HashSet<>();
                JsonNode compNameNode = iirNode.get(ElkUtils.ELK_IIR_COMPNAME);
                int count=0;
                if (null != compNameNode) {
                    for (JsonNode node : compNameNode) {
                        if (null != node) {
                            if(++count<=10){
                                compNames.add(node.asText());
                            }else{
                                break;
                            }
                        }
                    }
                }
                entity.setCompNames(compNames);
                entity.setKeyWords(params.getContent());
                listResult.add(entity);
            }
            return listResult;
        } catch (Exception e) {
            log.error("ELK检索研报返回内容:{}解析异常:{}", content, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }
    }

    //检查是否超时
    private boolean checkTimeOut(JsonNode readTree) {
        if (null != readTree) {
            JsonNode resultNode = readTree.get(ElkUtils.ELK_TIMED_OUT);
            if (null == resultNode) {
                return true;
            }
            return resultNode.asBoolean();
        }
        return true;
    }

    //组装公告查询参数
    private JSONObject getNoticeParams(NoticeReq notice) {

        //开始日期
        Long beginTime = null;
        //结束日期
        Long endTime = null;

        //查询时间类型 0全部 1近一个月 2近三个月 3近半年 4近一年 5自定义
        if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_ONE.equals(notice.getTimeType())) {
            String beginStr = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(),-1), DatePattern.NORM_DATE_PATTERN);
            String endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
            beginTime = DateUtil.parseDate(beginStr).getTime();
            endTime = DateUtil.parseDate(endStr).getTime() - 1;
        } else if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_THREE.equals(notice.getTimeType())) {
            String beginStr = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -3), DatePattern.NORM_DATE_PATTERN);
            String endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
            beginTime = DateUtil.parseDate(beginStr).getTime();
            endTime = DateUtil.parseDate(endStr).getTime() - 1;
        } else if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_SIX.equals(notice.getTimeType())) {
            String beginStr = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -6), DatePattern.NORM_DATE_PATTERN);
            String endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
            beginTime = DateUtil.parseDate(beginStr).getTime();
            endTime = DateUtil.parseDate(endStr).getTime() - 1;
        } else if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_YEAR.equals(notice.getTimeType())){
            String beginStr = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -12), DatePattern.NORM_DATE_PATTERN);
            String endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
            beginTime = DateUtil.parseDate(beginStr).getTime();
            endTime = DateUtil.parseDate(endStr).getTime() - 1;
        } else if (Constant.ELK_SEARCH_NOTICE_TIMETYPE_DEFINE.equals(notice.getTimeType())) {
            if (null == notice.getBeginDate() || null == notice.getEndDate()) {
                throw new ElkException(ElkErrorType.ELK_SEARCH_DATE_NULL);
            }

            String beginStr = DateUtil.format(notice.getBeginDate(), DatePattern.NORM_DATE_PATTERN);
            String endStr = DateUtil.format(DateUtil.offsetDay(notice.getEndDate(), 1), DatePattern.NORM_DATE_PATTERN);
            beginTime = DateUtil.parseDate(beginStr).getTime();
            endTime = DateUtil.parseDate(endStr).getTime() - 1;
        }

        //组装查询条件
        List<JSONObject> listTagsCondition = new ArrayList<>();
        if (null != notice.getTags() && !notice.getTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String data : notice.getTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TAGS, data);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, data);
                listTagsCondition.add(outerData);
            }
        }


        List<JSONObject> listContent = new ArrayList<>();
        List<JSONObject> listTitle = new ArrayList<>();
        //行业分类标签
        List<JSONObject> listIndustryCondition = new ArrayList<>();
        if (!notice.getIndustryTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : notice.getIndustryTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_INDUSTRYNAME, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listIndustryCondition.add(outerData);
            }
        }

        //公告类型
        List<JSONObject> listTypeCondition = new ArrayList<>();
        if (!notice.getNoticeTypeTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : notice.getNoticeTypeTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TYPE, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listTypeCondition.add(outerData);
            }
        }

        //公告类型
        if (!listTypeCondition.isEmpty()) {
            JSONObject typeShouldData = new JSONObject();
            typeShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listTypeCondition);
            JSONObject typeBoolData = new JSONObject();
            typeBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, typeShouldData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){   //标题
                listTitle.add(typeBoolData);
            }

            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){  //内容
                listContent.add(typeBoolData);
            }
        }

        //行业分类
        if (!listIndustryCondition.isEmpty()) {
            JSONObject rankShouldData = new JSONObject();
            rankShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listIndustryCondition);
            JSONObject rankBoolData = new JSONObject();
            rankBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, rankShouldData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){   //标题
                listTitle.add(rankBoolData);
            }

            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){  //内容
                listContent.add(rankBoolData);
            }
        }


        //时间条件
        if (beginTime != null && endTime != null) {
            JSONObject innerData = new JSONObject();
            innerData.put(ElkUtils.ELK_GRAMMAR_GTE, beginTime);
            innerData.put(ElkUtils.ELK_GRAMMAR_LTE, endTime);
            JSONObject outerData = new JSONObject();
            outerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_PUBLISHTIME, innerData);
            JSONObject rangeData = new JSONObject();
            rangeData.put(ElkUtils.ELK_GRAMMAR_RANGE, outerData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){   //标题
                listTitle.add(rangeData);
            }

            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){  //内容
                listContent.add(rangeData);
            }
        }


        if (!listTagsCondition.isEmpty()) {
            JSONObject mustShouldData = new JSONObject();
            mustShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listTagsCondition);
            JSONObject boolShouldData = new JSONObject();
            boolShouldData.put(ElkUtils.ELK_GRAMMAR_BOOL, mustShouldData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){   //标题
                listTitle.add(boolShouldData);
            }

            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){  //内容
                listContent.add(boolShouldData);
            }
        }


        //检索内容
        if(StringUtil.isNotBlank(notice.getContent())){
            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){   //标题
                //检索标题
                JSONObject titleInnerData = new JSONObject();
                titleInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TITLE, notice.getContent());
                JSONObject titleOuterData = new JSONObject();
                titleOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, titleInnerData);
                listTitle.add(titleOuterData);
            }

            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())){  //内容
                JSONObject contentInnerData = new JSONObject();
                contentInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_CONTENT, notice.getContent());
                JSONObject contentOuterData = new JSONObject();
                contentOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, contentInnerData);
                listContent.add(contentOuterData);
            }
        }

        List<JSONObject> listShould = new ArrayList<>();
        if(Constant.ELK_SEARCH_TYPE_TITLE.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())) {   //标题
            JSONObject titleMustData = new JSONObject();
            titleMustData.put(ElkUtils.ELK_GRAMMAR_MUST, listTitle);
            JSONObject titleBool = new JSONObject();
            titleBool.put(ElkUtils.ELK_GRAMMAR_BOOL, titleMustData);
            listShould.add(titleBool);
        }

        if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(notice.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(notice.getSearchType())) {  //内容
            JSONObject contentMustData = new JSONObject();
            contentMustData.put(ElkUtils.ELK_GRAMMAR_MUST, listContent);
            JSONObject contentBool = new JSONObject();
            contentBool.put(ElkUtils.ELK_GRAMMAR_BOOL, contentMustData);
            listShould.add(contentBool);
        }

        JSONObject shouldData = new JSONObject();
        shouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listShould);

        JSONObject boolData = new JSONObject();
        boolData.put(ElkUtils.ELK_GRAMMAR_BOOL, shouldData);
        JSONObject queryData = new JSONObject();
        queryData.put(ElkUtils.ELK_GRAMMAR_QUERY, boolData);

        //分页实现
        Integer size = notice.getSize() == null || notice.getSize() == 0 ? 20 : notice.getSize();
        //排序
        List<JSONObject> listSort = new ArrayList<>();
        JSONObject publishTimeSort = new JSONObject();
        publishTimeSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_PUBLISHTIME_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        JSONObject idSort = new JSONObject();
        idSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_ID_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        listSort.add(publishTimeSort);
        listSort.add(idSort);
        queryData.put(ElkUtils.ELK_GRAMMAR_SIZE,size);
        queryData.put(ElkUtils.ELK_GRAMMAR_SORT,listSort);

        //search_after
        if(!notice.getOrderNumber().isEmpty()){
            queryData.put(ElkUtils.ELK_GRAMMAR_SEARCH_AFTER,notice.getOrderNumber());
        }

        return queryData;
    }

    private JSONObject getCompInfoParams(CompSearchReq req){
        JSONObject queryData = new JSONObject();
        
        if (null != req.getCompName() && !req.getCompName().isEmpty()){
            JSONObject contentOuterData = new JSONObject();
            JSONObject boolData = new JSONObject();
            List<JSONObject> data = new ArrayList<>();
            JSONObject contentInnerData = new JSONObject();
            contentInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_COMP_NAME,req.getCompName());
            contentOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, contentInnerData);
            JSONObject asharedes =new JSONObject();
            JSONObject contentAsharedes = new JSONObject();
            asharedes.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_COMP_ASHAREDES_NAME_SHORT,req.getCompName());
            contentAsharedes.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,asharedes);
            JSONObject ashareint =new JSONObject();
            JSONObject contentAshareint = new JSONObject();
            ashareint.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_COMP_ASHAREINT_NAME_SHORT,req.getCompName());
            contentAshareint.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,ashareint);
            JSONObject cfundint =new JSONObject();
            JSONObject contentCfundint = new JSONObject();
            cfundint.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_COMP_CFUNDINT_NAME_SHORT,req.getCompName());
            contentCfundint.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,cfundint);
            JSONObject compintr =new JSONObject();
            JSONObject contentCompintr = new JSONObject();
            compintr.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_COMP_COMPINTR_NAME_SHORT,req.getCompName());
            contentCompintr.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,compintr);
            JSONObject bsharedes =new JSONObject();
            JSONObject contentBsharedes = new JSONObject();
            bsharedes.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_COMP_BSHAREDES_NAME_SHORT,req.getCompName());
            contentBsharedes.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,bsharedes);
            JSONObject bondCode =new JSONObject();
            JSONObject contentBondCode = new JSONObject();
            bondCode.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_COMP_BOND_CODE,"*"+req.getCompName()+"*");
            contentBondCode.put(ElkUtils.ELK_GRAMMAR_WILDCARD,bondCode);
            JSONObject bondName =new JSONObject();
            JSONObject contentBondName = new JSONObject();
            bondName.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_COMP_BOND_NAME,req.getCompName());
            contentBondName.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,bondName);
            JSONObject creditCode =new JSONObject();
            JSONObject contentCreditCode = new JSONObject();
            creditCode.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_COMP_CREDIT_CODE,"*"+req.getCompName()+"*");
            contentCreditCode.put(ElkUtils.ELK_GRAMMAR_WILDCARD,creditCode);
            data.add(contentOuterData);
            data.add(contentBondCode);
            data.add(contentBondName);
            data.add(contentAsharedes);
            data.add(contentAshareint);
            data.add(contentBsharedes);
            data.add(contentCfundint);
            data.add(contentCompintr);
            data.add(contentCreditCode);
            JSONObject shouldData = new JSONObject();
            shouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD,data);
            boolData.put(ElkUtils.ELK_GRAMMAR_BOOL,shouldData);
            queryData.put(ElkUtils.ELK_GRAMMAR_QUERY,boolData);
        }
        
        Integer size = req.getSize() == null || req.getSize() == 0 ? 20 : req.getSize();
        List<JSONObject> listSort = new ArrayList<>();
        JSONObject currenttimeSort = new JSONObject();
        JSONObject order = new JSONObject();
        order.put(ElkUtils.ELK_GRAMMAR_ORDER,ElkUtils.ELK_GRAMMAR_DESC);
        currenttimeSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_COMP_ID,order);
        JSONObject nameSort = new JSONObject();
        order.put(ElkUtils.ELK_GRAMMAR_ORDER,ElkUtils.ELK_GRAMMAR_ASC);
        nameSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_COMP_NAME,order);
        listSort.add(currenttimeSort);
        listSort.add(nameSort);
        queryData.put(ElkUtils.ELK_GRAMMAR_SIZE,size);
        queryData.put(ElkUtils.ELK_GRAMMAR_SORT,listSort);
    
        if(!req.getOrderNumber().isEmpty()){
            queryData.put(ElkUtils.ELK_GRAMMAR_SEARCH_AFTER,req.getOrderNumber());
        }
        log.info(JsonUtils.toJson(queryData));
        return queryData;
    }
    
    //获取新闻咨询查询参数
    private JSONObject getNewsInfoParams(NewsInfoReq newsInfo) {

        //组装查询条件
        List<JSONObject> listTagsCondition = new ArrayList<>();

        //开始日期
        Long beginTime = null;
        //结束日期
        Long endTime = null;

        //查询时间类型 0全部 1当天 2最近7天 3最近一个月 4自定义
        if (Constant.ELK_SEARCH_TIMETYPE_TODAY.equals(newsInfo.getTimeType())) {
            String beginStr = DateUtil.format(DateUtil.date(), DatePattern.NORM_DATE_PATTERN);
            String endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
            beginTime = DateUtil.parseDate(beginStr).getTime();
            endTime = DateUtil.parseDate(endStr).getTime() - 1;
        } else if (Constant.ELK_SEARCH_TIMETYPE_WEEK.equals(newsInfo.getTimeType())) {
            String beginStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), -6), DatePattern.NORM_DATE_PATTERN);
            String endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
            beginTime = DateUtil.parseDate(beginStr).getTime();
            endTime = DateUtil.parseDate(endStr).getTime() - 1;
        } else if (Constant.ELK_SEARCH_TIMETYPE_MONTH.equals(newsInfo.getTimeType())) {
            String beginStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), -29), DatePattern.NORM_DATE_PATTERN);
            String endStr = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 1), DatePattern.NORM_DATE_PATTERN);
            beginTime = DateUtil.parseDate(beginStr).getTime();
            endTime = DateUtil.parseDate(endStr).getTime() - 1;
        } else if (Constant.ELK_SEARCH_TIMETYPE_DEFINE.equals(newsInfo.getTimeType())) {
            if (null == newsInfo.getBeginDate() || null == newsInfo.getEndDate()) {
                throw new ElkException(ElkErrorType.ELK_SEARCH_DATE_NULL);
            }

            String beginStr = DateUtil.format(newsInfo.getBeginDate(), DatePattern.NORM_DATE_PATTERN);
            String endStr = DateUtil.format(DateUtil.offsetDay(newsInfo.getEndDate(), 1), DatePattern.NORM_DATE_PATTERN);
            beginTime = DateUtil.parseDate(beginStr).getTime();
            endTime = DateUtil.parseDate(endStr).getTime() - 1;
        }

        //组装标签条件
        if (!newsInfo.getTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : newsInfo.getTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TAGS, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listTagsCondition.add(outerData);
            }
        }

        //情感标签
        List<JSONObject> listSentimentCondition = new ArrayList<>();
        if (!newsInfo.getSentiments().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : newsInfo.getSentiments()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_SENTIMENT, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listSentimentCondition.add(outerData);
            }
        }

        List<JSONObject> listContent = new ArrayList<>();
        List<JSONObject> listTitle = new ArrayList<>();

        //情感标签
        if (!listSentimentCondition.isEmpty()) {
            JSONObject sentimentShouldData = new JSONObject();
            sentimentShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listSentimentCondition);
            JSONObject sentimentBoolData = new JSONObject();
            sentimentBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, sentimentShouldData);
            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){   //标题
                listTitle.add(sentimentBoolData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){  //内容
                listContent.add(sentimentBoolData);
            }
        }

        //时间条件
        if (beginTime != null && endTime != null) {
            JSONObject innerData = new JSONObject();
            innerData.put(ElkUtils.ELK_GRAMMAR_GTE, beginTime);
            innerData.put(ElkUtils.ELK_GRAMMAR_LTE, endTime);
            JSONObject outerData = new JSONObject();
            outerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_PUBLISHTIME, innerData);
            JSONObject rangeData = new JSONObject();
            rangeData.put(ElkUtils.ELK_GRAMMAR_RANGE, outerData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){   //标题
                listTitle.add(rangeData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){  //内容
                listContent.add(rangeData);
            }
        }

        if (!listTagsCondition.isEmpty()) {
            JSONObject mustShouldData = new JSONObject();
            mustShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listTagsCondition);
            JSONObject boolShouldData = new JSONObject();
            boolShouldData.put(ElkUtils.ELK_GRAMMAR_BOOL, mustShouldData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){   //标题
                listTitle.add(boolShouldData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){  //内容
                listContent.add(boolShouldData);
            }
        }

        if(StringUtil.isNotBlank(newsInfo.getContent())){

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){   //标题
                //检索标题
                JSONObject titleInnerData = new JSONObject();
                titleInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TITLE, newsInfo.getContent());
                JSONObject titleOuterData = new JSONObject();
                titleOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, titleInnerData);
                listTitle.add(titleOuterData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){  //内容
                //检索内容
                JSONObject contentInnerData = new JSONObject();
                contentInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_CONTENT, newsInfo.getContent());
                JSONObject contentOuterData = new JSONObject();
                contentOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, contentInnerData);
                listContent.add(contentOuterData);
            }
        }

        List<JSONObject> listShould = new ArrayList<>();
        if(Constant.ELK_SEARCH_TYPE_TITLE.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){   //标题
            JSONObject titleMustData = new JSONObject();
            titleMustData.put(ElkUtils.ELK_GRAMMAR_MUST, listTitle);
            JSONObject titleBool = new JSONObject();
            titleBool.put(ElkUtils.ELK_GRAMMAR_BOOL, titleMustData);
            listShould.add(titleBool);
        }
        if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){  //内容
            JSONObject contentMustData = new JSONObject();
            contentMustData.put(ElkUtils.ELK_GRAMMAR_MUST, listContent);
            JSONObject contentBool = new JSONObject();
            contentBool.put(ElkUtils.ELK_GRAMMAR_BOOL, contentMustData);
            listShould.add(contentBool);
        }

        JSONObject shouldData = new JSONObject();
        shouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listShould);

        JSONObject boolData = new JSONObject();
        boolData.put(ElkUtils.ELK_GRAMMAR_BOOL, shouldData);
        JSONObject queryData = new JSONObject();
        queryData.put(ElkUtils.ELK_GRAMMAR_QUERY, boolData);

        //分页实现
        Integer size = newsInfo.getSize() == null || newsInfo.getSize() == 0 ? 20 : newsInfo.getSize();
        //排序
        List<JSONObject> listSort = new ArrayList<>();
        JSONObject publishTimeSort = new JSONObject();
        publishTimeSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_PUBLISHTIME_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        JSONObject idSort = new JSONObject();
        idSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_ID_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        listSort.add(publishTimeSort);
        listSort.add(idSort);
        queryData.put(ElkUtils.ELK_GRAMMAR_SIZE,size);
        queryData.put(ElkUtils.ELK_GRAMMAR_SORT,listSort);

        //search_after
        if(!newsInfo.getOrderNumber().isEmpty()){
            queryData.put(ElkUtils.ELK_GRAMMAR_SEARCH_AFTER,newsInfo.getOrderNumber());
        }
        return queryData;
    }

    //获取行业资讯查询参数
    private JSONObject getIndutryNewsInfoParams(IndustryNewsInfoReq newsInfo) {

        //组装查询条件

        //标签条件
        List<JSONObject> listTagsCondition = new ArrayList<>();
        if (!newsInfo.getTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : newsInfo.getTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TAGS, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listTagsCondition.add(outerData);
            }
        }

        //情感标签
        List<JSONObject> listSentimentCondition = new ArrayList<>();
        if (!newsInfo.getSentiments().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : newsInfo.getSentiments()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_SENTIMENT, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listSentimentCondition.add(outerData);
            }
        }

        List<JSONObject> listTitle = new ArrayList<>();

        //情感标签
        if (!listSentimentCondition.isEmpty()) {
            JSONObject sentimentShouldData = new JSONObject();
            sentimentShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listSentimentCondition);
            JSONObject sentimentBoolData = new JSONObject();
            sentimentBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, sentimentShouldData);
            listTitle.add(sentimentBoolData);

        }


        if (!listTagsCondition.isEmpty()) {
            JSONObject mustShouldData = new JSONObject();
            mustShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listTagsCondition);
            JSONObject boolShouldData = new JSONObject();
            boolShouldData.put(ElkUtils.ELK_GRAMMAR_BOOL, mustShouldData);
            listTitle.add(boolShouldData);

        }

        //搜索框检索
        List<JSONObject> listContent = new ArrayList<>();
        if(StringUtil.isNotBlank(newsInfo.getContent())){
            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){   //标题
                //检索标题
                JSONObject titleInnerData = new JSONObject();
                titleInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TITLE, newsInfo.getContent());
                JSONObject titleOuterData = new JSONObject();
                titleOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, titleInnerData);
                listContent.add(titleOuterData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(newsInfo.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(newsInfo.getSearchType())){  //内容
                //检索内容
                JSONObject contentInnerData = new JSONObject();
                contentInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_CONTENT, newsInfo.getContent());
                JSONObject contentOuterData = new JSONObject();
                contentOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, contentInnerData);
                listContent.add(contentOuterData);
            }
        }
        if(!listContent.isEmpty()){
            JSONObject contentShouldData = new JSONObject();
            contentShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listContent);
            JSONObject contentBoolData = new JSONObject();
            contentBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, contentShouldData);
            listTitle.add(contentBoolData);
        }

        //行业检索
        List<JSONObject> listIndutry = new ArrayList<>();
        if(StringUtil.isNotBlank(newsInfo.getIndutryCode()) && newsInfo.getIndutryLevel() != null){
            String indutryCode = null;
            if(Constant.ELK_SEARCH_ONE_INDUTRY.equals(newsInfo.getIndutryLevel().toString())){
                indutryCode = newsInfo.getIndutryCode().substring(0,2);
            }else if(Constant.ELK_SEARCH_TWO_INDUTRY.equals(newsInfo.getIndutryLevel().toString())){
                indutryCode = newsInfo.getIndutryCode().substring(0,4);
            }else if(Constant.ELK_SEARCH_THREE_INDUTRY.equals(newsInfo.getIndutryLevel().toString())){
                indutryCode = newsInfo.getIndutryCode().substring(0,6);
            }else if(Constant.ELK_SEARCH_FOUR_INDUTRY.equals(newsInfo.getIndutryLevel().toString())){
                indutryCode = newsInfo.getIndutryCode();
            }

            //检索内容
            JSONObject indutryInnerData = new JSONObject();
            indutryInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_INDUSTRYCODE,indutryCode+"*");
            JSONObject contentOuterData = new JSONObject();
            contentOuterData.put(ElkUtils.ELK_GRAMMAR_WILDCARD, indutryInnerData);
            listIndutry.add(contentOuterData);

        }
        if(!listIndutry.isEmpty()){
            JSONObject indutryShouldData = new JSONObject();
            indutryShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listIndutry);
            JSONObject indutryBoolData = new JSONObject();
            indutryBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, indutryShouldData);
            listTitle.add(indutryBoolData);
        }

        //数据类型
        List<JSONObject> listDatatype = new ArrayList<>();
        JSONObject dataTypeInnerData = new JSONObject();
        dataTypeInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_DATA_TYPE,'1');
        JSONObject dataTypeOuterData = new JSONObject();
        dataTypeOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, dataTypeInnerData);
        listDatatype.add(dataTypeOuterData);
        if(!listDatatype.isEmpty()){
            JSONObject dataTypeShouldData = new JSONObject();
            dataTypeShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listDatatype);
            JSONObject dataTypeBoolData = new JSONObject();
            dataTypeBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, dataTypeShouldData);
            listTitle.add(dataTypeBoolData);
        }


        List<JSONObject> listShould = new ArrayList<>();
        JSONObject titleMustData = new JSONObject();
        titleMustData.put(ElkUtils.ELK_GRAMMAR_MUST, listTitle);
        JSONObject titleBool = new JSONObject();
        titleBool.put(ElkUtils.ELK_GRAMMAR_BOOL, titleMustData);
        listShould.add(titleBool);

        JSONObject shouldData = new JSONObject();
        shouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listShould);

        JSONObject boolData = new JSONObject();
        boolData.put(ElkUtils.ELK_GRAMMAR_BOOL, shouldData);
        JSONObject queryData = new JSONObject();
        queryData.put(ElkUtils.ELK_GRAMMAR_QUERY, boolData);

        //分页实现
        Integer size = newsInfo.getSize() == null || newsInfo.getSize() == 0 ? 20 : newsInfo.getSize();
        //排序
        List<JSONObject> listSort = new ArrayList<>();
        JSONObject publishTimeSort = new JSONObject();
        publishTimeSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_PUBLISHTIME_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        JSONObject idSort = new JSONObject();
        idSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_ID_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        listSort.add(publishTimeSort);
        listSort.add(idSort);
        queryData.put(ElkUtils.ELK_GRAMMAR_SIZE,size);
        queryData.put(ElkUtils.ELK_GRAMMAR_SORT,listSort);

        //search_after
        if(!newsInfo.getOrderNumber().isEmpty()){
            queryData.put(ElkUtils.ELK_GRAMMAR_SEARCH_AFTER,newsInfo.getOrderNumber());
        }
        return queryData;
    }

    //获取专家观点查询参数
    private JSONObject getIndutryPointsInfoParams(IndustryPointsInfoReq pointsInfoReq) {

        //组装查询条件

        //标签条件
        List<JSONObject> listTagsCondition = new ArrayList<>();
        if (!pointsInfoReq.getTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : pointsInfoReq.getTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TAGS, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listTagsCondition.add(outerData);
            }
        }

        //情感标签
        List<JSONObject> listSentimentCondition = new ArrayList<>();
        if (!pointsInfoReq.getSentiments().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : pointsInfoReq.getSentiments()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_SENTIMENT, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listSentimentCondition.add(outerData);
            }
        }

        List<JSONObject> listTitle = new ArrayList<>();

        //情感标签
        if (!listSentimentCondition.isEmpty()) {
            JSONObject sentimentShouldData = new JSONObject();
            sentimentShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listSentimentCondition);
            JSONObject sentimentBoolData = new JSONObject();
            sentimentBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, sentimentShouldData);
            listTitle.add(sentimentBoolData);

        }


        if (!listTagsCondition.isEmpty()) {
            JSONObject mustShouldData = new JSONObject();
            mustShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listTagsCondition);
            JSONObject boolShouldData = new JSONObject();
            boolShouldData.put(ElkUtils.ELK_GRAMMAR_BOOL, mustShouldData);
            listTitle.add(boolShouldData);

        }

        //搜索框检索
        List<JSONObject> listContent = new ArrayList<>();
        if(StringUtil.isNotBlank(pointsInfoReq.getContent())){
            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(pointsInfoReq.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(pointsInfoReq.getSearchType())){   //标题
                //检索标题
                JSONObject titleInnerData = new JSONObject();
                titleInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TITLE, pointsInfoReq.getContent());
                JSONObject titleOuterData = new JSONObject();
                titleOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, titleInnerData);
                listContent.add(titleOuterData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(pointsInfoReq.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(pointsInfoReq.getSearchType())){  //内容
                //检索内容
                JSONObject contentInnerData = new JSONObject();
                contentInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_CONTENT, pointsInfoReq.getContent());
                JSONObject contentOuterData = new JSONObject();
                contentOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, contentInnerData);
                listContent.add(contentOuterData);
            }
        }
        if(!listContent.isEmpty()){
            JSONObject contentShouldData = new JSONObject();
            contentShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listContent);
            JSONObject contentBoolData = new JSONObject();
            contentBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, contentShouldData);
            listTitle.add(contentBoolData);
        }

        //行业检索
        List<JSONObject> listIndutry = new ArrayList<>();
        if(StringUtil.isNotBlank(pointsInfoReq.getIndutryCode()) && pointsInfoReq.getIndutryLevel() != null){
            String indutryCode = null;
            if(Constant.ELK_SEARCH_ONE_INDUTRY.equals(pointsInfoReq.getIndutryLevel().toString())){
                indutryCode = pointsInfoReq.getIndutryCode().substring(0,2);
            }else if(Constant.ELK_SEARCH_TWO_INDUTRY.equals(pointsInfoReq.getIndutryLevel().toString())){
                indutryCode = pointsInfoReq.getIndutryCode().substring(0,4);
            }else if(Constant.ELK_SEARCH_THREE_INDUTRY.equals(pointsInfoReq.getIndutryLevel().toString())){
                indutryCode = pointsInfoReq.getIndutryCode().substring(0,6);
            }else if(Constant.ELK_SEARCH_FOUR_INDUTRY.equals(pointsInfoReq.getIndutryLevel().toString())){
                indutryCode = pointsInfoReq.getIndutryCode();
            }

            //检索内容
            JSONObject indutryInnerData = new JSONObject();
            indutryInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_INDUSTRYCODE,indutryCode+"*");
            JSONObject contentOuterData = new JSONObject();
            contentOuterData.put(ElkUtils.ELK_GRAMMAR_WILDCARD, indutryInnerData);
            listIndutry.add(contentOuterData);

        }
        if(!listIndutry.isEmpty()){
            JSONObject indutryShouldData = new JSONObject();
            indutryShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listIndutry);
            JSONObject indutryBoolData = new JSONObject();
            indutryBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, indutryShouldData);
            listTitle.add(indutryBoolData);
        }

        List<JSONObject> listShould = new ArrayList<>();
        JSONObject titleMustData = new JSONObject();
        titleMustData.put(ElkUtils.ELK_GRAMMAR_MUST, listTitle);
        JSONObject titleBool = new JSONObject();
        titleBool.put(ElkUtils.ELK_GRAMMAR_BOOL, titleMustData);
        listShould.add(titleBool);

        JSONObject shouldData = new JSONObject();
        shouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listShould);

        JSONObject boolData = new JSONObject();
        boolData.put(ElkUtils.ELK_GRAMMAR_BOOL, shouldData);
        JSONObject queryData = new JSONObject();
        queryData.put(ElkUtils.ELK_GRAMMAR_QUERY, boolData);

        //分页实现
        Integer size = pointsInfoReq.getSize() == null || pointsInfoReq.getSize() == 0 ? 20 : pointsInfoReq.getSize();
        //排序
        List<JSONObject> listSort = new ArrayList<>();
        JSONObject publishTimeSort = new JSONObject();
        publishTimeSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_PUBLISHTIME_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        JSONObject idSort = new JSONObject();
        idSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_ID_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        listSort.add(publishTimeSort);
        listSort.add(idSort);
        queryData.put(ElkUtils.ELK_GRAMMAR_SIZE,size);
        queryData.put(ElkUtils.ELK_GRAMMAR_SORT,listSort);

        //search_after
        if(!pointsInfoReq.getOrderNumber().isEmpty()){
            queryData.put(ElkUtils.ELK_GRAMMAR_SEARCH_AFTER,pointsInfoReq.getOrderNumber());
        }
        return queryData;
    }

    //获取研报查询参数
    private JSONObject getReportParams(ReportReq report) {

        //组装查询条件
        List<JSONObject> listTypeCondition = new ArrayList<>();
        //研报类型
        if (!report.getReportTypeTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : report.getReportTypeTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TYPE, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listTypeCondition.add(outerData);
            }
        }

        //评级标签
        List<JSONObject> listRankCondition = new ArrayList<>();
        if (!report.getRankTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : report.getRankTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_RANK, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listRankCondition.add(outerData);
            }
        }

        //评级标签
        List<JSONObject> listRankChangeCondition = new ArrayList<>();
        if (!report.getRankChangeTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : report.getRankChangeTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_RANKCHANGE, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listRankChangeCondition.add(outerData);
            }
        }
    
    
        //来源机构
        List<JSONObject> listSourceAgency = new ArrayList<>();
        if (null != report.getSourceAgency() && !report.getSourceAgency().isEmpty()){
            JSONObject innerData = null;
            JSONObject outerData = null;
            for (String s : report.getSourceAgency()) {
                innerData = new JSONObject();
                outerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_SOURCE,s);
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,innerData);
                listSourceAgency.add(outerData);
            }
        }
        //行业标签
        List<JSONObject> listIndustryCondition = new ArrayList<>();
        if (!report.getIndustryTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : report.getIndustryTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_INDUSTRYNAME, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listIndustryCondition.add(outerData);
            }
        }

        //数据来源
        List<JSONObject> listDataSource = new ArrayList<>();
        if (null!= report.getDataSource() && !report.getDataSource().isEmpty()){
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String s : report.getDataSource()) {
                innerData = new JSONObject();
                outerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_DATA_SOURCE,s);
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,innerData);
                listDataSource.add(outerData);
            }
        }

        List<JSONObject> listContent = new ArrayList<>();
        List<JSONObject> listTitle = new ArrayList<>();


        
        //类型
        if (!listTypeCondition.isEmpty()) {
            JSONObject typeShouldData = new JSONObject();
            typeShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listTypeCondition);
            JSONObject typeBoolData = new JSONObject();
            typeBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, typeShouldData);
            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){   //标题
                listTitle.add(typeBoolData);
            }

            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){  //内容
                listContent.add(typeBoolData);
            }
        }

        //评级
        if (!listRankCondition.isEmpty()) {
            JSONObject rankShouldData = new JSONObject();
            rankShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listRankCondition);
            JSONObject rankBoolData = new JSONObject();
            rankBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, rankShouldData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){   //标题
                listTitle.add(rankBoolData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){  //内容
                listContent.add(rankBoolData);
            }
        }

        //评级
        if (!listRankChangeCondition.isEmpty()) {
            JSONObject rankChangeShouldData = new JSONObject();
            rankChangeShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listRankChangeCondition);
            JSONObject rankChangeBoolData = new JSONObject();
            rankChangeBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, rankChangeShouldData);
            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){   //标题
                listTitle.add(rankChangeBoolData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){  //内容
                listContent.add(rankChangeBoolData);
            }
        }

        //行业
        if (!listIndustryCondition.isEmpty()) {
            JSONObject industryShouldData = new JSONObject();
            industryShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listIndustryCondition);
            JSONObject industryBoolData = new JSONObject();
            industryBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, industryShouldData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){   //标题
                listTitle.add(industryBoolData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){  //内容
                listContent.add(industryBoolData);
            }
        }

        //数据来源
        if (!listDataSource.isEmpty()){
            JSONObject dataSourceShouldData = new JSONObject();
            dataSourceShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD,listDataSource);
            JSONObject dataSourceBoolData = new JSONObject();
            dataSourceBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL,dataSourceShouldData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){   //标题
                listTitle.add(dataSourceBoolData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){  //内容
                listContent.add(dataSourceBoolData);
            }
        }
        
        //来源机构
        if (!listSourceAgency.isEmpty()) {
            JSONObject sourceAgencyShouldData = new JSONObject();
            sourceAgencyShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD,listSourceAgency);
            JSONObject sourceAgencyBoolData = new JSONObject();
            sourceAgencyBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL,sourceAgencyShouldData);

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){   //标题
                listTitle.add(sourceAgencyBoolData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){  //内容
                listContent.add(sourceAgencyBoolData);
            }
        }
        
        
        if(StringUtil.isNotBlank(report.getContent())){

            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){   //标题
                //检索标题
                JSONObject titleInnerData = new JSONObject();
                titleInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TITLE, report.getContent());
                JSONObject titleOuterData = new JSONObject();
                titleOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, titleInnerData);
                listTitle.add(titleOuterData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){  //内容
                //检索内容
                JSONObject contentInnerData = new JSONObject();
                contentInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_CONTENT, report.getContent());
                JSONObject contentOuterData = new JSONObject();
                contentOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, contentInnerData);
                listContent.add(contentOuterData);
            }
        }
        if(null != report.getPages()){
            JSONObject pages = new JSONObject();
            pages.put("iir.iir_pages",report.getPages());
            JSONObject pagesMacth = new JSONObject();
            pagesMacth.put("match_phrase",pages);
            listContent.add(pagesMacth);
            listTitle.add(pagesMacth);
        }
        List<JSONObject> listShould = new ArrayList<>();
        if(Constant.ELK_SEARCH_TYPE_TITLE.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){   //标题
            JSONObject titleMustData = new JSONObject();
            titleMustData.put(ElkUtils.ELK_GRAMMAR_MUST, listTitle);
            JSONObject titleBool = new JSONObject();
            titleBool.put(ElkUtils.ELK_GRAMMAR_BOOL, titleMustData);
            listShould.add(titleBool);
        }
        if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(report.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(report.getSearchType())){  //内容
            JSONObject contentMustData = new JSONObject();
            contentMustData.put(ElkUtils.ELK_GRAMMAR_MUST, listContent);
            JSONObject contentBool = new JSONObject();
            contentBool.put(ElkUtils.ELK_GRAMMAR_BOOL, contentMustData);
            listShould.add(contentBool);
        }

        JSONObject shouldData = new JSONObject();
        shouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listShould);
        JSONObject boolData = new JSONObject();
        boolData.put(ElkUtils.ELK_GRAMMAR_BOOL, shouldData);

        JSONObject queryData = new JSONObject();
        queryData.put(ElkUtils.ELK_GRAMMAR_QUERY, boolData);

        //分页实现
        Integer size = report.getSize() == null || report.getSize() == 0 ? 20 : report.getSize();
        //排序
        List<JSONObject> listSort = new ArrayList<>();
        JSONObject publishTimeSort = new JSONObject();
        publishTimeSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_PUBLISHTIME_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        JSONObject idSort = new JSONObject();
        idSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_ID_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        listSort.add(publishTimeSort);
        listSort.add(idSort);
        queryData.put(ElkUtils.ELK_GRAMMAR_SIZE,size);
        queryData.put(ElkUtils.ELK_GRAMMAR_SORT,listSort);

        //search_after
        if(!report.getOrderNumber().isEmpty()){
            queryData.put(ElkUtils.ELK_GRAMMAR_SEARCH_AFTER,report.getOrderNumber());
        }
        return queryData;
    }

    //获取原标签
    private Set<String> handleTags(String content) {
        Set<String> setTags = new HashSet<>();
        JsonNode readTree = null;
        try {
            readTree = OBJECT_MAPPER.readTree(content);
        } catch (Exception e) {
            log.error("原标签内容解析失败:{}", e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }

        //校验文档是否存在
        JsonNode foundNode = readTree.get(ElkUtils.ELK_FOUND);
        if (null == foundNode) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        } else {
            boolean result = foundNode.asBoolean();
            if (!result) {
                throw new ElkException(ElkErrorType.Elk_SEARCH_GETDOC_NULL);
            }
        }

        //获取_source内容
        JsonNode sourceNode = readTree.get(ElkUtils.ELK_SOURCE);
        if (null == sourceNode) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }

        //获取iir内容
        JsonNode iirNode = sourceNode.get(ElkUtils.ELK_IIR);
        if (null == iirNode) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_CONTENT_ANALYSIS_EXCEPTION);
        }

        //获取标签
        JsonNode tagNode = iirNode.get(ElkUtils.ELK_IIR_TAGS);
        if (null != tagNode) {
            for (JsonNode node : tagNode) {
                if (null != node) {
                    setTags.add(node.asText());
                }
            }
        }
        return setTags;
    }

    //组装修改索引标签的参数
    private JSONObject getUpdateParams(Set<String> setTags) {
        JSONObject fieldData = new JSONObject();
        fieldData.put(ElkUtils.ELK_IIR_SELF_TAGS, setTags);
        JSONObject iirData = new JSONObject();
        iirData.put(ElkUtils.ELK_IIR, fieldData);
        JSONObject docData = new JSONObject();
        docData.put(ElkUtils.ELK_GRAMMAR_DOC, iirData);
        return docData;
    }

    //获取ELK检索url
    private String getElkSearchUrl(String indexName) {
        String url = null;
        if (StringUtil.isBlank(elkUrl) || StringUtil.isBlank(indexName)) {
            throw new ElkException(ElkErrorType.ELK_URL_INDEX_BLANK, elkUrl, indexName);
        }
        if (elkUrl.endsWith("/") || elkUrl.endsWith("\\")) {
            url = elkUrl + indexName + "/" + ElkUtils.ELK_GRAMMAR_SEARCH;
        } else {
            url = elkUrl + "/" + indexName + "/" + ElkUtils.ELK_GRAMMAR_SEARCH;
        }
        return url;
    }

    //通过ELKID获取数据URL
    private String getElkSearchByIdUrl(String indexName, String id) {
        String url = null;
        if (StringUtil.isBlank(elkUrl) || StringUtil.isBlank(indexName)) {
            throw new ElkException(ElkErrorType.ELK_URL_INDEX_BLANK, elkUrl, indexName);
        }
        if (elkUrl.endsWith("/") || elkUrl.endsWith("\\")) {
            url = elkUrl + indexName + "/" + ElkUtils.ELK_GRAMMAR_TYPE_DOC + "/" + id;
        } else {
            url = elkUrl + "/" + indexName + "/" + ElkUtils.ELK_GRAMMAR_TYPE_DOC + "/" + id;
        }
        return url;
    }


    //获取ELK更新URL
    private String getElkSearchUpdateUrl(String indexName, String id) {
        String url = null;
        if (StringUtil.isBlank(elkUrl) || StringUtil.isBlank(indexName)) {
            throw new ElkException(ElkErrorType.ELK_URL_INDEX_BLANK, elkUrl, indexName);
        }
        if (elkUrl.endsWith("/") || elkUrl.endsWith("\\")) {
            url = elkUrl + indexName + "/" + ElkUtils.ELK_GRAMMAR_TYPE_DOC + "/" + id + "/" + ElkUtils.ELK_GRAMMAR_UPDATE;
        } else {
            url = elkUrl + "/" + indexName + "/" + ElkUtils.ELK_GRAMMAR_TYPE_DOC + "/" + id + "/" + ElkUtils.ELK_GRAMMAR_UPDATE;
        }
        return url;
    }

    /**
     * 获取短内容
     * @param content
     * @param keyWords
     * @return
     */
    private String getShortContent(String content,String keyWords){
        if(StringUtil.isBlank(content)){
            return null;
        }
        int length = content.length();
        if(StringUtil.isBlank(keyWords)){
            if(length>=300){
                return content.substring(0,300).trim();
            }
            return content;
        }

        String result = null;
        Matcher matcher = Pattern.compile(keyWords).matcher(content);
        if (matcher.find()) {
            int start = matcher.start();
            if(start>30){
                int sublength = length-start-30;
                if(sublength>=300){
                    result = content.substring(start-30,start+270).trim();
                }else{
                    result = content.substring(start-30).trim();
                }
            }else{
                if(length>=300){
                    result = content.substring(0,300);
                }else{
                    result = content;
                }
            }
        } else {
            if(length>=300){
                result = content.substring(0,300).trim();
            }else{
                result = content;
            }
        }
        return result;
    }
    
    private String getShortContent1(String content,String keyWords){
        if(StringUtil.isBlank(content)){
            return null;
        }
        int length = content.length();
        if(StringUtil.isBlank(keyWords)){
            if(length>=180){
                return content.substring(0,180).trim();
            }
            return content;
        }
        
        String result = null;
        Matcher matcher = Pattern.compile(keyWords).matcher(content);
        if (matcher.find()) {
            int start = matcher.start();
            if(start>30){
                int sublength = length-start-30;
                if(sublength>=180){
                    result = content.substring(start-30,start+150).trim();
                }else{
                    result = content.substring(start-30).trim();
                }
            }else{
                if(length>=180){
                    result = content.substring(0,180);
                }else{
                    result = content;
                }
            }
        } else {
            if(length>=180){
                result = content.substring(0,180).trim();
            }else{
                result = content;
            }
        }
        return result;
    }
    
    
    @Override
    public void deleteElkData(DeleteElkDataReqVO vo) {
        String elkDeleteUrl = null;
        
        if (vo.getType() ==1){
             elkDeleteUrl = getElkDeleteUrl(indexNoticeName);
        }
        if (vo.getType()==2){
             elkDeleteUrl = getElkDeleteUrl(indexNoticeName);
        }
        if (vo.getType() == 3){
           elkDeleteUrl = getElkDeleteUrl(indexReportName);
        }
        JSONObject delete = new JSONObject();
        JSONObject query = new JSONObject();
        JSONObject data = new JSONObject();
        for (String s : vo.getId().split(",")) {
            data.put(ElkUtils.ELK_IIR_CLICKHOUSEID,s);
            query.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,data);
            delete.put(ElkUtils.ELK_GRAMMAR_QUERY,query);
            try {
                MY_HTTP_CLIENT.postJson(elkDeleteUrl,delete);
            }catch (Exception e){
                throw new ElkException(ElkErrorType.ELK_DELETE_EXCEPTION,e);
            }
        }
        
    }
    
    
    private String getElkDeleteUrl(String indexName) {
        String url = null;
        if (StringUtil.isBlank(elkUrl) || StringUtil.isBlank(indexName)) {
            throw new ElkException(ElkErrorType.ELK_URL_INDEX_BLANK, elkUrl, indexName);
        }
        if (elkUrl.endsWith("/") || elkUrl.endsWith("\\")) {
            url = elkUrl + indexName + "/" + ElkUtils.ELK_GRAMMAR_TYPE_DOC + "/" + ElkUtils.ELK_GRAMMAR_DELETE;
        } else {
            url = elkUrl + "/" + indexName + "/" + ElkUtils.ELK_GRAMMAR_TYPE_DOC + "/" + ElkUtils.ELK_GRAMMAR_DELETE;
        }
        return url;
    }
    @Autowired
    private UserService userService;
    
    @Autowired
    private DictService dictService;
    
    @Autowired
    private ReportTypeService reportTypeService;
    
    @Autowired
    private ReportCollectService collectService;

    @Autowired
    private MaterialCollectService materialCollectService;

    @Autowired
    private MaterialService materialService;
    
    @Autowired
    private ReportService reportService;
    
    @Autowired
    private IndustrySwService industrySwService;
    
    @Override
    public List<SearchPrivateReportVO> searchPrivateReport(ReportReq reportReq) {
        JSONObject params = this.getPrivateReportParams(reportReq);
        String url;
        if (StringUtil.isBlank(elkUrl) || StringUtil.isBlank(indexPrivateReport)) {
            throw new ElkException(ElkErrorType.ELK_URL_INDEX_BLANK, elkUrl, indexPrivateReport);
        }
        if (elkUrl.endsWith("/") || elkUrl.endsWith("\\")) {
            url = elkUrl + indexPrivateReport +  "/_search";
        } else {
            url = elkUrl + "/" + indexPrivateReport  + "/_search";
        }
        log.info("内部研报检索条件{}",params.toString());
        String s = MY_HTTP_CLIENT.postJson(url, params);
        try{
            JsonNode jsonNode = OBJECT_MAPPER.readTree(s);
            long count = jsonNode.get("hits").get("total").get("value").asLong();
            
            List<SearchPrivateReportVO> result = new ArrayList();
            List<Map<String, JsonNode>> iirListObject = getIirListObject(jsonNode);
            if (iirListObject.isEmpty()) {
                return result;
            }
            Map<String, Dict> reportLevel = dictService.queryDict("REPORT_LEVEL");
            Map<String, Dict> rankChange = dictService.queryDict("LEVEL_CHANGE");
            for (Map<String, JsonNode> mapNode : iirListObject) {
                SearchPrivateReportVO entity = new SearchPrivateReportVO();
                JsonNode iirNode = mapNode.get(ElkUtils.ELK_IIR);
                entity.setId(iirNode.get("iir_mysql_id").asLong());
                entity.setTitle(iirNode.get("iir_title").asText());
                String ss = iirNode.get("iir_dow_tag").asText();
                int i1 = Integer.parseInt(ss);
                entity.setDowTag(i1);
                if (iirNode.get("iir_industry_name") != null){
                    IndustrySw sw = industrySwService
                            .queryByCode(iirNode.get("iir_industry_name").asText());
                    entity.setIndustryName(sw.getName());
                }
                
                entity.setOrgName(iirNode.get("iir_org_name").asText());
                entity.setAnalyst(iirNode.get("iir_analyst").asText());
                if (iirNode.get("iir_pages") != null){
                    entity.setPages(Integer.parseInt(iirNode.get("iir_pages").asText()));
                }else {
                    entity.setPages(0);
                }
                
                entity.setComments(iirNode.get("iir_comments").asInt());
                entity.setUser(userService.queryById(iirNode.get("iir_user_id").asLong()).getName());
                if (iirNode.get("iir_publishtime") != null){
                    entity.setPublishTime(new Date(iirNode.get("iir_publishtime").asLong()));
                }
                
                entity.setCreateTime(ElkUtils.getDate(iirNode, "iir_currenttime"));
                entity.setCount(count);
                String shortContent = this.getShortContent1(ElkUtils.getString(iirNode,"iir_summary"),reportReq.getContent());
                entity.setSummary(shortContent);
                ReportCollect reportCollect = new ReportCollect();
                reportCollect.setReportId(entity.getId());
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                entity.setCreateUserId(loginUser.getId());
                List<ReportCollect> reportCollects = collectService.queryByReportId(entity.getId(),entity.getCreateUserId());
                if (reportCollects.isEmpty()) {
                    entity.setCollection(2);
                }else {
                    entity.setCollection(1);
                }
                if (iirNode.get("iir_size") != null){
                    long size = iirNode.get("iir_size").asLong();
                    entity.setFileSize(reportService.getPrintSize(size));
                }
                if (iirNode.get("iir_file_path") != null){
                   entity.setFilePath(minioAddress+iirNode.get("iir_file_path").asText());
                }
                if(iirNode.get("iir_rank") != null){
                    Integer i = iirNode.get("iir_rank").asInt();
                    if (dictService.queryDictByCode(reportLevel,i.toString()) != null) {
                        entity.setRank(dictService.queryDictByCode(reportLevel,i.toString()).getBusiName());
                    }
                    
                }else {
                    entity.setRank("");
                }
                if (iirNode.get("iir_rank_change")  != null && dictService.queryDictByCode(rankChange,iirNode.get("iir_rank_change").asText()) != null){
                    entity.setRankChange(dictService.queryDictByCode(rankChange,iirNode.get("iir_rank_change").asText()).getBusiName());
                }else {
                    entity.setRankChange("");
                }
                
                
                if (iirNode.get("iir_self_tag") != null){
                    List<String> selfTags = new ArrayList<>();
                    JsonNode selfTagNode = iirNode.get("iir_self_tag");
                    if (null != selfTagNode) {
                        for (JsonNode node : selfTagNode) {
                            if (null != node) {
                                selfTags.add(node.asText());
                            }
                        }
                    }
                    entity.setTags(selfTags);
                }else {
                    entity.setTags(new ArrayList<>());
                }

                if (iirNode.get("iir_report_type") != null){
                    Long l = iirNode.get("iir_report_type").asLong();
                    entity.setReportType(reportTypeService.queryById(l).getName());
                }
                //获取排序码
                List<String> sorts = new ArrayList<>();
                JsonNode sortNode = mapNode.get(ElkUtils.ELK_SORT);
                if (null != sortNode) {
                    for (JsonNode node : sortNode) {
                        if (null != node) {
                            sorts.add(node.asText());
                        }
                    }
                }
                entity.setSort(sorts);
                entity.setKeyWords(reportReq.getContent());
                result.add(entity);
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            
        }
        return  new ArrayList();
    }
    
    private JSONObject getPrivateReportParams(ReportReq reportReq) {
        Long  start = null;
        Long end = null;
        if (Constant.COMP_RISK_TIME_DAY.equals(reportReq.getType())) {
           String beginTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN);
           String endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
          start= DateUtil.parse(beginTime).getTime();
          end = DateUtil.parse(endTime).getTime();
        } else if (Constant.COMP_RISK_TIME_WEEK.equals(reportReq.getType())) {
           String beginTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), -3), DatePattern.NORM_DATE_PATTERN);
           String  endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
            start= DateUtil.parse(beginTime).getTime();
            end = DateUtil.parse(endTime).getTime();
        } else if (Constant.COMP_RISK_TIME_MONTH.equals(reportReq.getType())) {
           String beginTime = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -7), DatePattern.NORM_DATE_PATTERN);
           String endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
            start= DateUtil.parse(beginTime).getTime();
            end = DateUtil.parse(endTime).getTime();
        }else if (reportReq.getType() == 4){
            String beginTime = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -1), DatePattern.NORM_DATE_PATTERN);
           String endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
            start= DateUtil.parse(beginTime).getTime();
            end = DateUtil.parse(endTime).getTime();
        }else if (reportReq.getType() == 5){
            String beginTime = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -3), DatePattern.NORM_DATE_PATTERN);
           String endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
            start= DateUtil.parse(beginTime).getTime();
            end = DateUtil.parse(endTime).getTime();
        }else if (reportReq.getType() == 6){
            if (null == reportReq.getStart() || null == reportReq.getEnd()) {
                throw new ElkException(ElkErrorType.ELK_SEARCH_DATE_NULL);
            }
            start= DateUtil.parse(reportReq.getStart()).getTime();
            end = DateUtil.parse(reportReq.getEnd()+" 23:59:59").getTime();
            
        }
        List<JSONObject> list = new ArrayList<>();
        if (start != null && end != null){
            JSONObject innerData = new JSONObject();
            innerData.put(ElkUtils.ELK_GRAMMAR_GTE, start);
            innerData.put(ElkUtils.ELK_GRAMMAR_LTE, end);
            JSONObject time = new JSONObject();
            time.put("iir.iir_currenttime",innerData);
            JSONObject range = new JSONObject();
            range.put(ElkUtils.ELK_GRAMMAR_RANGE,time);
            list.add(range);
        }
        
        
        if (!StringUtil.isBlank(reportReq.getContent())){
            if (reportReq.getSearchType().equals("0")){
                JSONObject title = new JSONObject();
                title.put("iir.iir_title",reportReq.getContent());
                JSONObject m = new JSONObject();
                m.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,title);
                JSONObject content = new JSONObject();
                content.put("iir.iir_summary",reportReq.getContent());
                JSONObject m1 = new JSONObject();
                m1.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,content);
                JSONObject titleWild = new JSONObject();
                titleWild.put("iir.iir_title","*"+reportReq.getContent()+"*");
                JSONObject m2 = new JSONObject();
                m2.put(ElkUtils.ELK_GRAMMAR_WILDCARD,titleWild);
                List<JSONObject> l = new ArrayList<>();
                l.add(m);
                l.add(m1);
                l.add(m2);
                JSONObject s =new JSONObject();
                s.put(ElkUtils.ELK_GRAMMAR_SHOULD,l);

                JSONObject b = new JSONObject();
                b.put("bool",s);
                list.add(b);

            }

            if (reportReq.getSearchType().equals("1")){
                JSONObject title = new JSONObject();
                title.put("iir.iir_title",reportReq.getContent());
                JSONObject m = new JSONObject();
                m.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,title);
                JSONObject titleWild = new JSONObject();
                titleWild.put("iir.iir_title","*"+reportReq.getContent()+"*");
                JSONObject m2 = new JSONObject();
                m2.put(ElkUtils.ELK_GRAMMAR_WILDCARD,titleWild);
                List<JSONObject> l = new ArrayList<>();
                l.add(m);
                l.add(m2);
                JSONObject s =new JSONObject();
                s.put(ElkUtils.ELK_GRAMMAR_SHOULD,l);

                JSONObject b = new JSONObject();
                b.put("bool",s);
                list.add(b);
            }
    
        }

        //添加查询status=0的数据
        JSONObject status = new JSONObject();
        status.put("iir.iir_status","0");
        JSONObject sta = new JSONObject();
        sta.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,status);
        List<JSONObject> mustData = new ArrayList<>();
        mustData.add(sta);
        JSONObject mustSome =new JSONObject();
        mustSome.put(ElkUtils.ELK_GRAMMAR_MUST,mustData);

        JSONObject bMust = new JSONObject();
        bMust.put("bool",mustSome);
        list.add(bMust);
        
    
        //组装查询条件
        List<JSONObject> listTypeCondition = new ArrayList<>();
        //研报类型
        if (!reportReq.getReportTypeTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : reportReq.getReportTypeTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + "iir_report_type", content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listTypeCondition.add(outerData);
            }
        }
        if (reportReq.getUser() != null){
            List<JSONObject> ll = new ArrayList<>();
            for (Long l : reportReq.getUser()) {
                JSONObject uu = new JSONObject();
                uu.put("iir.iir_user_id",l);
                JSONObject m11 = new JSONObject();
                m11.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,uu);
                ll.add(m11);
            }
            JSONObject s111 = new JSONObject();
            s111.put("should",ll);
            JSONObject mm = new JSONObject();
            mm.put("bool",s111);
            list.add(mm);
        }
        if(!listTypeCondition.isEmpty()){
            JSONObject typeShoul = new JSONObject();
            typeShoul.put(ElkUtils.ELK_GRAMMAR_SHOULD,listTypeCondition);
            JSONObject typeBool = new JSONObject();
            typeBool.put("bool",typeShoul);
            list.add(typeBool);
        }
        //评级标签
        List<JSONObject> listRankCondition = new ArrayList<>();
        if (!reportReq.getRankTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : reportReq.getRankTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_RANK, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listRankCondition.add(outerData);
            }
        }
        if (!listRankCondition.isEmpty()){
            JSONObject rankShould = new JSONObject();
            rankShould.put(ElkUtils.ELK_GRAMMAR_SHOULD,listRankCondition);
            JSONObject rankBool = new JSONObject();
            rankBool.put("bool",rankShould);
            list.add(rankBool);
        }
        
        //来源机构
        List<JSONObject> listSourceAgency = new ArrayList<>();
        if (null != reportReq.getDataSource() && !reportReq.getDataSource().isEmpty()){
            JSONObject innerData = null;
            JSONObject outerData = null;
            for (String s : reportReq.getDataSource()) {
                innerData = new JSONObject();
                outerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR+".iir_org_name",s);
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,innerData);
                listSourceAgency.add(outerData);
            }
        }
        if(!listSourceAgency.isEmpty()){
            JSONObject source = new JSONObject();
            source.put(ElkUtils.ELK_GRAMMAR_SHOULD,listSourceAgency);
            JSONObject sourceBool = new JSONObject();
            sourceBool.put("bool",source);
            list.add(sourceBool);
         }
        
        //行业标签
        List<JSONObject> listIndustryCondition = new ArrayList<>();
        if (!reportReq.getIndustryTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : reportReq.getIndustryTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + ".iir_industry_name" , content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listIndustryCondition.add(outerData);
            }
        }
        if (!listIndustryCondition.isEmpty()){
            JSONObject i = new JSONObject();
            i.put(ElkUtils.ELK_GRAMMAR_SHOULD,listIndustryCondition);
            JSONObject iBool = new JSONObject();
            iBool.put("bool",i);
            list.add(iBool);
        }
        //页数
        if(1==reportReq.getPages()){
            JSONObject inner = new JSONObject();
            inner.put(ElkUtils.ELK_GRAMMAR_GTE, 20);
            JSONObject out = new JSONObject();
            out.put("iir.iir_pages",inner);
            JSONObject rangePage = new JSONObject();
            rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE,out);
            list.add(rangePage);
        }else if(2==reportReq.getPages()){
            JSONObject inner = new JSONObject();
            inner.put(ElkUtils.ELK_GRAMMAR_GTE, 10);
            inner.put(ElkUtils.ELK_GRAMMAR_LTE,20);
            JSONObject out = new JSONObject();
            out.put("iir.iir_pages",inner);
            JSONObject rangePage = new JSONObject();
            rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE,out);
            list.add(rangePage);
        }else if(3==reportReq.getPages()){
            JSONObject inner = new JSONObject();
            inner.put(ElkUtils.ELK_GRAMMAR_LTE,10);
            JSONObject out = new JSONObject();
            out.put("iir.iir_pages",inner);
            JSONObject rangePage = new JSONObject();
            rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE,out);
            list.add(rangePage);
        }else if(4==reportReq.getPages()){
            JSONObject inner = new JSONObject();
            inner.put(ElkUtils.ELK_GRAMMAR_LTE,5);
            JSONObject out = new JSONObject();
            out.put("iir.iir_pages",inner);
            JSONObject rangePage = new JSONObject();
            rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE,out);
            list.add(rangePage);
        }
        //分析师
        List<JSONObject> analystsList = new ArrayList<>();
        if(!reportReq.getAnalysts().isEmpty()){
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : reportReq.getAnalysts()) {
                innerData = new JSONObject();
                innerData.put("iir.iir_analyst.keyword", content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                analystsList.add(outerData);
            }
        }
        if (!analystsList.isEmpty()){
            JSONObject a = new JSONObject();
            a.put(ElkUtils.ELK_GRAMMAR_SHOULD,analystsList);
            JSONObject b =new JSONObject();
            b.put("bool",a);
            list.add(b);
        }
        //公司
        if (reportReq.getComp() != null && !reportReq.getComp().equals("")){
            JSONObject c =new JSONObject();
            c.put("iir.iir_comp_name",reportReq.getComp());
            JSONObject cs = new JSONObject();
            cs.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,c);
            list.add(cs);
        }
    
     
        //当前用户是否有权限查看，添加用户id时要加上上传的id
        List<JSONObject> user = new ArrayList<>();
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        Long id = loginUser.getId();
        JSONObject u = new JSONObject();
        u.put("iir.iir_tag_id",id);
        JSONObject us = new JSONObject();
        us.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,u);
        user.add(us);
        JSONObject c = new JSONObject();
        c.put("iir.iir_user_id",loginUser.getId());
        JSONObject mc = new JSONObject();
        mc.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,c);
        JSONObject tag = new JSONObject();
        user.add(mc);
        tag.put("iir.iir_right_tag",1);
        JSONObject tm = new JSONObject();
        tm.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,tag);
        user.add(tm);
        JSONObject ss = new JSONObject();
        ss.put("should",user);
        JSONObject bb = new JSONObject();
        bb.put("bool",ss);
        list.add(bb);
        JSONObject queryMust = new JSONObject();
        queryMust.put("must",list);
        JSONObject queryBool = new JSONObject();
        queryBool.put("bool",queryMust);
        
    
        JSONObject params = new JSONObject();
        params.put("query",queryBool);
        params.put("track_total_hits",true);
        
        //分页实现
        Integer size = reportReq.getSize() == null || reportReq.getSize() == 0 ? 20 : reportReq.getSize();
        //排序
        List<JSONObject> listSort = new ArrayList<>();
        JSONObject publishTimeSort = new JSONObject();
        publishTimeSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_CURRENTTIME,ElkUtils.ELK_GRAMMAR_DESC);
        listSort.add(publishTimeSort);
        JSONObject idSort = new JSONObject();
        //idSort.put("iir.iir_mysql_id","desc");
       // listSort.add(idSort);
        params.put(ElkUtils.ELK_GRAMMAR_SIZE,size);
        params.put(ElkUtils.ELK_GRAMMAR_SORT,listSort);
    
        //search_after
        if(!reportReq.getOrderNumber().isEmpty()){
            params.put(ElkUtils.ELK_GRAMMAR_SEARCH_AFTER,reportReq.getOrderNumber());
        }
        log.info("params:"+params);
        return params;
    }
    
    @Override
    public void updatePrivateReport(List<Long> users,PrivateReport report) {
        
        String u = elkUrl+indexPrivateReport+"_update_by_query";
        JSONObject params = new JSONObject();
        JSONObject source = new JSONObject();
        source.put("source","ctx._source.iir['iir_tag_id'] = '"+users+"';ctx._source.iir['iir_right_tag']'='"+report.getRightTag()+"'");
        JSONObject m = new JSONObject();
        JSONObject f = new JSONObject();
        f.put("iir.mysql_id",report.getId());
        m.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,f);
        params.put("query",m);
        params.put("script",source);
        log.info("修改参数",params.toString());
        MY_HTTP_CLIENT.postJson(u,params);
    }

    @Override
    public List<SearchPrivateMaterialVO> searchPrivateMaterial(MaterialReq materialReq) {
        JSONObject params = this.getPrivateMaterialParams(materialReq);
        String url;
        if (StringUtil.isBlank(elkUrl) || StringUtil.isBlank(indexMeansName)) {
            throw new ElkException(ElkErrorType.ELK_URL_INDEX_BLANK, elkUrl, indexMeansName);
        }
        if (elkUrl.endsWith("/") || elkUrl.endsWith("\\")) {
            url = elkUrl + indexMeansName +  "/_search";
        } else {
            url = elkUrl + "/" + indexMeansName  + "/_search";
        }
        log.info("内部资料检索条件{}",params.toString());
        String s = MY_HTTP_CLIENT.postJson(url, params);
        log.info("url解析数据:",s);
        try{
            JsonNode jsonNode = OBJECT_MAPPER.readTree(s);
            long count = jsonNode.get("hits").get("total").get("value").asLong();

            List<SearchPrivateMaterialVO> result = new ArrayList();
            List<Map<String, JsonNode>> iirListObject = getIirListObject(jsonNode);
            if (iirListObject.isEmpty()) {
                return result;
            }

            for (Map<String, JsonNode> mapNode : iirListObject) {
                SearchPrivateMaterialVO entity = new SearchPrivateMaterialVO();
                JsonNode iirNode = mapNode.get(ElkUtils.ELK_IIR);
                entity.setId(iirNode.get("iir_mysql_id").asLong());
                entity.setTitle(iirNode.get("iir_title").asText());
                String ss = iirNode.get("iir_dow_tag").asText();
                int i1 = Integer.parseInt(ss);
                entity.setDowTag(i1);
                if (iirNode.get("iir_industry_name") != null){
                    IndustrySw sw = industrySwService
                            .queryByCode(iirNode.get("iir_industry_name").asText());
                    entity.setIndustryName(sw.getName());
                }

                if(iirNode.get("iir_dept_name") != null){
                    entity.setPublishDeptName(iirNode.get("iir_dept_name").asText());
                }

                entity.setCreateTime(ElkUtils.getDate(iirNode, "iir_currenttime"));
                if(iirNode.get("iir_deptid") != null){
                    entity.setPublishParentDeptName(
                            (!"华能贵诚信托有限公司".equals(departmentService.queryById(departmentService.queryById(iirNode.get("iir_deptid").asLong()).getParentId()).getName()) &&
                                    (!"华能贵诚".equals(departmentService.queryById(departmentService.queryById(iirNode.get("iir_deptid").asLong()).getParentId()).getName()))) ? departmentService.queryById(iirNode.get("iir_parent_deptid").asLong()).getName() : "");
                }
                if (iirNode.get("iir_pages") != null){
                    entity.setPages(Integer.parseInt(iirNode.get("iir_pages").asText()));
                }else {
                    entity.setPages(0);
                }
                log.info("=====组装数据======");
                entity.setComments(ElkUtils.getInt(iirNode,"iir_comments"));
                if(iirNode.get("iir_publish_userid") != null){
                    entity.setUser(userService.queryById(iirNode.get("iir_publish_userid").asLong()).getName());
                }
                if (iirNode.get("iir_publishtime") != null){
                    entity.setPublishTime(new Date(iirNode.get("iir_publishtime").asLong()));
                }

                entity.setCount(count);
                String shortContent = this.getShortContent1(ElkUtils.getString(iirNode,"iir_summary"),materialReq.getContent());
                entity.setSummary(shortContent);
                MaterialCollect materialCollect = new MaterialCollect();
                materialCollect.setReportId(entity.getId());
                User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
                entity.setCreateUserId(loginUser.getId());
                List<MaterialCollect> materialCollects = materialCollectService.queryByReportId(entity.getId(),entity.getCreateUserId());
                if (materialCollects.isEmpty()) {
                    entity.setCollection(2);
                }else {
                    entity.setCollection(1);
                }
                if (iirNode.get("iir_size") != null){
                    long size = iirNode.get("iir_size").asLong();
                    entity.setFileSize(materialService.getPrintSize(size));
                }
                if (iirNode.get("iir_file_path") != null){
                    entity.setFilePath(minioAddress+iirNode.get("iir_file_path").asText());
                }

                Map<String, Dict> materialType = dictService.queryDict("MATERIAL_TYPE");

                if (iirNode.get("iir_material_type")  != null && dictService.queryDictByCode(materialType,iirNode.get("iir_material_type").asText()) != null){
                    entity.setMaterialType(dictService.queryDictByCode(materialType,iirNode.get("iir_material_type").asText()).getBusiName());
                }else {
                    entity.setMaterialType("");
                }

                //获取排序码
                List<String> sorts = new ArrayList<>();
                JsonNode sortNode = mapNode.get(ElkUtils.ELK_SORT);
                if (null != sortNode) {
                    for (JsonNode node : sortNode) {
                        if (null != node) {
                            sorts.add(node.asText());
                        }
                    }
                }
                entity.setSort(sorts);
                entity.setKeyWords(materialReq.getContent());
                result.add(entity);
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();

        }
        return  new ArrayList();
    }

    /**
     * 行业资讯检索
     * @param newsInfoReq
     * @return
     */
    @Override
    public List<IndustryNewsInfoResp> searchIndustryNewsInfo(IndustryNewsInfoReq newsInfoReq) {
        //获取连接
        String httpUrl = getElkSearchUrl(indexNewsName);

        //获取查询参数
        JSONObject queryParams = getIndutryNewsInfoParams(newsInfoReq);
        OptLogReqVO vo = new OptLogReqVO();
//        vo.setOptContent("输入"+params.getContent()+"检索新闻资讯");
        vo.setOptType(3);
        vo.setOptModule(5);
        try {
            log.info("查询参数:{}",queryParams.toString());
            String content = MY_HTTP_CLIENT.postJson(httpUrl, queryParams);
            vo.setStatus(1);
            logService.saveOptLog(vo);
            return handleIndutryNewsInfoResult(content,newsInfoReq);
        } catch (Exception e) {
            vo.setStatus(2);
            logService.saveOptLog(vo);
            log.error("ELK检索行业资讯索引地址:{}异常:{}", httpUrl, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_INDEX_EXCEPTION, httpUrl);
        }
    }

    /**
     * 行业研报检索
     * @param reportReq
     * @return
     */
    @Override
    public List<IndustryReportResp> searchIndustryReportInfo(IndustryReportReq reportReq) {
        //获取连接
        String httpUrl = getElkSearchUrl(indexReportName);

        //获取研报查询参数
        JSONObject queryParams = getIndustryReportParams(reportReq);
        OptLogReqVO vo = new OptLogReqVO();
        vo.setOptContent("输入"+reportReq.getContent()+"检索研究报告");
        vo.setOptType(3);
        vo.setOptModule(4);
        String s = queryParams.toString();
        try {
            log.info("查询参数:{}",s);
            String content = MY_HTTP_CLIENT.postJson(httpUrl, queryParams);
            vo.setStatus(1);
            logService.saveOptLog(vo);
            return this.handleIndustryReportResult(content,reportReq);
        } catch (Exception e) {
            log.error("ELK检索研报索引地址:{}异常:{}", httpUrl, e);
            vo.setStatus(2);
            logService.saveOptLog(vo);
            throw new ElkException(ElkErrorType.ELK_SEARCH_INDEX_EXCEPTION, httpUrl);
        }
    }

    @Override
    public List<IndustryPointsInfoResp> searchIndustryPointsInfo(IndustryPointsInfoReq pointsInfoReq) {
        //获取连接
        String httpUrl = getElkSearchUrl(indexPointName);

        //获取查询参数
        JSONObject queryParams = getIndutryPointsInfoParams(pointsInfoReq);
        OptLogReqVO vo = new OptLogReqVO();
//        vo.setOptContent("输入"+params.getContent()+"检索新闻资讯");
        vo.setOptType(3);
        vo.setOptModule(5);
        try {
            log.info("查询参数:{}",queryParams.toString());
            String content = MY_HTTP_CLIENT.postJson(httpUrl, queryParams);
            vo.setStatus(1);
            logService.saveOptLog(vo);
            return handleIndutryPointsInfoResult(content,pointsInfoReq);
        } catch (Exception e) {
            vo.setStatus(2);
            logService.saveOptLog(vo);
            log.error("ELK检索行业资讯索引地址:{}异常:{}", httpUrl, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_INDEX_EXCEPTION, httpUrl);
        }
    }

    @Override
    public void updatePointTags(String id, Set<String> tags) {
        if (StringUtil.isBlank(id)) {
            throw new ElkException(ElkErrorType.ELK_SEARCH_TAGS_ID_NULL);
        }
        //查询原数据URL
        String queryUrl = this.getElkSearchByIdUrl(indexPointName, id);
        //原内容
        String content = null;
        try {
            content = MY_HTTP_CLIENT.get(queryUrl);
        } catch (Exception e) {
            log.error("获取专家观点原文档失败索引ID:{}异常信息:{}", id, e);
            throw new ElkException(ElkErrorType.ELK_SEARCH_GETDOC_ERROR);
        }

        //修改数据URL
        String updateUrl = this.getElkSearchUpdateUrl(indexPointName, id);
        //修改标签
        try {
            MY_HTTP_CLIENT.postJson(updateUrl, getUpdateParams(tags));
        } catch (Exception e) {
            log.error("修改ELK专家观点文档标签失败:{}", e);
            throw new ElkException(ElkErrorType.Elk_SEARCH_GETDOC_UPDATE_ERROR);
        }
    }

    private JSONObject getIndustryReportParams(IndustryReportReq reportReq) {

        //组装查询条件
        List<JSONObject> listTypeCondition = new ArrayList<>();
        //研报类型
        if (!reportReq.getReportTypeTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : reportReq.getReportTypeTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TYPE, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listTypeCondition.add(outerData);
            }
        }

        //评级标签
        List<JSONObject> listRankCondition = new ArrayList<>();
        if (!reportReq.getRankTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : reportReq.getRankTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_RANK, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listRankCondition.add(outerData);
            }
        }

        //评级标签
        List<JSONObject> listRankChangeCondition = new ArrayList<>();
        if (!reportReq.getRankChangeTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : reportReq.getRankChangeTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_RANKCHANGE, content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listRankChangeCondition.add(outerData);
            }
        }


        //来源机构
        List<JSONObject> listSourceAgency = new ArrayList<>();
        if (null != reportReq.getSourceAgency() && !reportReq.getSourceAgency().isEmpty()){
            JSONObject innerData = null;
            JSONObject outerData = null;
            for (String s : reportReq.getSourceAgency()) {
                innerData = new JSONObject();
                outerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_SOURCE,s);
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,innerData);
                listSourceAgency.add(outerData);
            }
        }

        //行业检索
        List<JSONObject> listIndutry = new ArrayList<>();
        if(StringUtil.isNotBlank(reportReq.getIndutryCode()) && reportReq.getIndutryLevel() != null){
            String indutryCode = null;
            if(Constant.ELK_SEARCH_ONE_INDUTRY.equals(reportReq.getIndutryLevel().toString())){
                indutryCode = reportReq.getIndutryCode().substring(0,2);
            }else if(Constant.ELK_SEARCH_TWO_INDUTRY.equals(reportReq.getIndutryLevel().toString())){
                indutryCode = reportReq.getIndutryCode().substring(0,4);
            }else if(Constant.ELK_SEARCH_THREE_INDUTRY.equals(reportReq.getIndutryLevel().toString())){
                indutryCode = reportReq.getIndutryCode().substring(0,6);
            }else if(Constant.ELK_SEARCH_FOUR_INDUTRY.equals(reportReq.getIndutryLevel().toString())){
                indutryCode = reportReq.getIndutryCode();
            }

            //检索内容
            JSONObject indutryInnerData = new JSONObject();
            indutryInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_INDUSTRYCODE,indutryCode+"*");
            JSONObject contentOuterData = new JSONObject();
            contentOuterData.put(ElkUtils.ELK_GRAMMAR_WILDCARD, indutryInnerData);
            listIndutry.add(contentOuterData);

        }

        //数据来源
        List<JSONObject> listDataSource = new ArrayList<>();
        if (null!= reportReq.getDataSource() && !reportReq.getDataSource().isEmpty()){
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String s : reportReq.getDataSource()) {
                innerData = new JSONObject();
                outerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_IIR_DATA_SOURCE,s);
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,innerData);
                listDataSource.add(outerData);
            }
        }

        List<JSONObject> listTitle = new ArrayList<>();



        //研报类型
        if (!listTypeCondition.isEmpty()) {
            JSONObject typeShouldData = new JSONObject();
            typeShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listTypeCondition);
            JSONObject typeBoolData = new JSONObject();
            typeBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, typeShouldData);
            listTitle.add(typeBoolData);

        }

        //评级
        if (!listRankCondition.isEmpty()) {
            JSONObject rankShouldData = new JSONObject();
            rankShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listRankCondition);
            JSONObject rankBoolData = new JSONObject();
            rankBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, rankShouldData);
            listTitle.add(rankBoolData);
        }

        //评级
        if (!listRankChangeCondition.isEmpty()) {
            JSONObject rankChangeShouldData = new JSONObject();
            rankChangeShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listRankChangeCondition);
            JSONObject rankChangeBoolData = new JSONObject();
            rankChangeBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, rankChangeShouldData);
            listTitle.add(rankChangeBoolData);

        }

        //行业
        if(!listIndutry.isEmpty()){
            JSONObject indutryShouldData = new JSONObject();
            indutryShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listIndutry);
            JSONObject indutryBoolData = new JSONObject();
            indutryBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, indutryShouldData);
            listTitle.add(indutryBoolData);
        }


        //数据来源
        if (!listDataSource.isEmpty()){
            JSONObject dataSourceShouldData = new JSONObject();
            dataSourceShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD,listDataSource);
            JSONObject dataSourceBoolData = new JSONObject();
            dataSourceBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL,dataSourceShouldData);
            listTitle.add(dataSourceBoolData);

        }

        //来源机构
        if (!listSourceAgency.isEmpty()) {
            JSONObject sourceAgencyShouldData = new JSONObject();
            sourceAgencyShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD,listSourceAgency);
            JSONObject sourceAgencyBoolData = new JSONObject();
            sourceAgencyBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL,sourceAgencyShouldData);
            listTitle.add(sourceAgencyBoolData);

        }

        //搜索框检索
        List<JSONObject> listContent = new ArrayList<>();
        if(StringUtil.isNotBlank(reportReq.getContent())){
            if(Constant.ELK_SEARCH_TYPE_TITLE.equals(reportReq.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(reportReq.getSearchType())){   //标题
                //检索标题
                JSONObject titleInnerData = new JSONObject();
                titleInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_TITLE, reportReq.getContent());
                JSONObject titleOuterData = new JSONObject();
                titleOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, titleInnerData);
                listContent.add(titleOuterData);
            }
            if(Constant.ELK_SEARCH_TYPE_CONTENT.equals(reportReq.getSearchType()) || Constant.ELK_SEARCH_TYPE_ALL.equals(reportReq.getSearchType())){  //内容
                //检索内容
                JSONObject contentInnerData = new JSONObject();
                contentInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_CONTENT, reportReq.getContent());
                JSONObject contentOuterData = new JSONObject();
                contentOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, contentInnerData);
                listContent.add(contentOuterData);
            }
        }
        if(!listContent.isEmpty()){
            JSONObject contentShouldData = new JSONObject();
            contentShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listContent);
            JSONObject contentBoolData = new JSONObject();
            contentBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, contentShouldData);
            listTitle.add(contentBoolData);
        }

        //数据类型
        List<JSONObject> listDatatype = new ArrayList<>();
        JSONObject dataTypeInnerData = new JSONObject();
        dataTypeInnerData.put(ElkUtils.ELK_IIR + "." + ElkUtils.ELK_IIR_DATA_TYPE,'1');
        JSONObject dataTypeOuterData = new JSONObject();
        dataTypeOuterData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, dataTypeInnerData);
        listDatatype.add(dataTypeOuterData);
        if(!listDatatype.isEmpty()){
            JSONObject dataTypeShouldData = new JSONObject();
            dataTypeShouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listDatatype);
            JSONObject dataTypeBoolData = new JSONObject();
            dataTypeBoolData.put(ElkUtils.ELK_GRAMMAR_BOOL, dataTypeShouldData);
            listTitle.add(dataTypeBoolData);
        }


        //页数
        if(null != reportReq.getPages()) {
            if (1 == reportReq.getPages()) {
                JSONObject pages = new JSONObject();
                pages.put(ElkUtils.ELK_GRAMMAR_GTE, 20);
                JSONObject pagesMacth = new JSONObject();
                pagesMacth.put("iir.iir_pages", pages);
                JSONObject rangePage = new JSONObject();
                rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE, pagesMacth);
                listTitle.add(rangePage);
            } else if (2 == reportReq.getPages()) {
                JSONObject pages = new JSONObject();
                pages.put(ElkUtils.ELK_GRAMMAR_GTE, 10);
                pages.put(ElkUtils.ELK_GRAMMAR_LTE, 20);
                JSONObject pagesMacth = new JSONObject();
                pagesMacth.put("iir.iir_pages", pages);
                JSONObject rangePage = new JSONObject();
                rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE, pagesMacth);
                listTitle.add(rangePage);
            } else if (3 == reportReq.getPages()) {
                JSONObject pages = new JSONObject();
                pages.put(ElkUtils.ELK_GRAMMAR_LTE, 10);
                JSONObject pagesMacth = new JSONObject();
                pagesMacth.put("iir.iir_pages", pages);
                JSONObject rangePage = new JSONObject();
                rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE, pagesMacth);
                listTitle.add(rangePage);
            }

        }

        List<JSONObject> listShould = new ArrayList<>();
        JSONObject titleMustData = new JSONObject();
        titleMustData.put(ElkUtils.ELK_GRAMMAR_MUST, listTitle);
        JSONObject titleBool = new JSONObject();
        titleBool.put(ElkUtils.ELK_GRAMMAR_BOOL, titleMustData);
        listShould.add(titleBool);


        JSONObject shouldData = new JSONObject();
        shouldData.put(ElkUtils.ELK_GRAMMAR_SHOULD, listShould);
        JSONObject boolData = new JSONObject();
        boolData.put(ElkUtils.ELK_GRAMMAR_BOOL, shouldData);

        JSONObject queryData = new JSONObject();
        queryData.put(ElkUtils.ELK_GRAMMAR_QUERY, boolData);

        //分页实现
        Integer size = reportReq.getSize() == null || reportReq.getSize() == 0 ? 20 : reportReq.getSize();
        //排序
        List<JSONObject> listSort = new ArrayList<>();
        JSONObject publishTimeSort = new JSONObject();
        publishTimeSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_PUBLISHTIME_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        JSONObject idSort = new JSONObject();
        idSort.put(ElkUtils.ELK_IIR+"."+ElkUtils.ELK_ORDER_IIR_ID_KEYWORD,ElkUtils.ELK_GRAMMAR_DESC);
        listSort.add(publishTimeSort);
        listSort.add(idSort);
        queryData.put(ElkUtils.ELK_GRAMMAR_SIZE,size);
        queryData.put(ElkUtils.ELK_GRAMMAR_SORT,listSort);

        //search_after
        if(!reportReq.getOrderNumber().isEmpty()){
            queryData.put(ElkUtils.ELK_GRAMMAR_SEARCH_AFTER,reportReq.getOrderNumber());
        }
        return queryData;

    }

    private JSONObject getPrivateMaterialParams(MaterialReq materialReq) {
        Long  start = null;
        Long end = null;
        if (Constant.COMP_RISK_TIME_DAY.equals(materialReq.getType())) {
            String beginTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN);
            String endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
            start= DateUtil.parse(beginTime).getTime();
            end = DateUtil.parse(endTime).getTime();
        } else if (Constant.COMP_RISK_TIME_WEEK.equals(materialReq.getType())) {
            String beginTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), -3), DatePattern.NORM_DATE_PATTERN);
            String  endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
            start= DateUtil.parse(beginTime).getTime();
            end = DateUtil.parse(endTime).getTime();
        } else if (Constant.COMP_RISK_TIME_MONTH.equals(materialReq.getType())) {
            String beginTime = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -7), DatePattern.NORM_DATE_PATTERN);
            String endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
            start= DateUtil.parse(beginTime).getTime();
            end = DateUtil.parse(endTime).getTime();
        }else if (materialReq.getType() == 4){
            String beginTime = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -1), DatePattern.NORM_DATE_PATTERN);
            String endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
            start= DateUtil.parse(beginTime).getTime();
            end = DateUtil.parse(endTime).getTime();
        }else if (materialReq.getType() == 5){
            String beginTime = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -3), DatePattern.NORM_DATE_PATTERN);
            String endTime = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), 0), DatePattern.NORM_DATE_PATTERN)+" 23:59:59";
            start= DateUtil.parse(beginTime).getTime();
            end = DateUtil.parse(endTime).getTime();
        }else if (materialReq.getType() == 6){
            if (null == materialReq.getStart() || null == materialReq.getEnd()) {
                throw new ElkException(ElkErrorType.ELK_SEARCH_DATE_NULL);
            }
            start= DateUtil.parse(materialReq.getStart()).getTime();
            end = DateUtil.parse(materialReq.getEnd()+" 23:59:59").getTime();

        }
        List<JSONObject> list = new ArrayList<>();
        if (start != null && end != null){
            JSONObject innerData = new JSONObject();
            innerData.put(ElkUtils.ELK_GRAMMAR_GTE, start);
            innerData.put(ElkUtils.ELK_GRAMMAR_LTE, end);
            JSONObject time = new JSONObject();
            time.put("iir.iir_publishtime",innerData);
            JSONObject range = new JSONObject();
            range.put(ElkUtils.ELK_GRAMMAR_RANGE,time);
            list.add(range);
        }


        if (!StringUtil.isBlank(materialReq.getContent())){
            if (materialReq.getSearchType().equals("0")){
                JSONObject title = new JSONObject();
                title.put("iir.iir_title",materialReq.getContent());
                JSONObject m = new JSONObject();
                m.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,title);
                JSONObject content = new JSONObject();
                content.put("iir.iir_summary",materialReq.getContent());
                JSONObject m1 = new JSONObject();
                m1.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,content);
                JSONObject titleWild = new JSONObject();
                titleWild.put("iir.iir_title","*"+materialReq.getContent()+"*");
                JSONObject m2 = new JSONObject();
                m2.put(ElkUtils.ELK_GRAMMAR_WILDCARD,titleWild);
                List<JSONObject> l = new ArrayList<>();
                l.add(m);
                l.add(m1);
                l.add(m2);
                JSONObject s =new JSONObject();
                s.put(ElkUtils.ELK_GRAMMAR_SHOULD,l);

                JSONObject b = new JSONObject();
                b.put("bool",s);
                list.add(b);

            }

            if (materialReq.getSearchType().equals("1")){
                JSONObject title = new JSONObject();
                title.put("iir.iir_title",materialReq.getContent());
                JSONObject m = new JSONObject();
                m.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,title);
                JSONObject titleWild = new JSONObject();
                titleWild.put("iir.iir_title","*"+materialReq.getContent()+"*");
                JSONObject m2 = new JSONObject();
                m2.put(ElkUtils.ELK_GRAMMAR_WILDCARD,titleWild);
                List<JSONObject> l = new ArrayList<>();
                l.add(m);
                l.add(m2);
                JSONObject s =new JSONObject();
                s.put(ElkUtils.ELK_GRAMMAR_SHOULD,l);

                JSONObject b = new JSONObject();
                b.put("bool",s);
                list.add(b);
            }

        }

        //添加查询status=0的数据
        JSONObject status = new JSONObject();
        status.put("iir.iir_status","0");
        JSONObject sta = new JSONObject();
        sta.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,status);
        JSONObject outerDa = new JSONObject();
        JSONObject innerDa = new JSONObject();
        //资料类型
        if (materialReq.getMaterialType() != null && !materialReq.getMaterialType().equals("")) {
            innerDa.put(ElkUtils.ELK_IIR + "." + "iir_material_type", materialReq.getMaterialType());
            outerDa.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerDa);
        }

        List<JSONObject> mustData = new ArrayList<>();
        mustData.add(sta);
        mustData.add(outerDa);
        JSONObject mustSome =new JSONObject();
        mustSome.put(ElkUtils.ELK_GRAMMAR_MUST,mustData);

        JSONObject bMust = new JSONObject();
        bMust.put("bool",mustSome);
        list.add(bMust);


        //组装查询条件


        //行业标签
        List<JSONObject> listIndustryCondition = new ArrayList<>();
        if (!materialReq.getIndustryTags().isEmpty()) {
            JSONObject outerData = null;
            JSONObject innerData = null;
            for (String content : materialReq.getIndustryTags()) {
                innerData = new JSONObject();
                innerData.put(ElkUtils.ELK_IIR + ".iir_industry_name" , content);
                outerData = new JSONObject();
                outerData.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, innerData);
                listIndustryCondition.add(outerData);
            }
        }
        if (!listIndustryCondition.isEmpty()){
            JSONObject i = new JSONObject();
            i.put(ElkUtils.ELK_GRAMMAR_SHOULD,listIndustryCondition);
            JSONObject iBool = new JSONObject();
            iBool.put("bool",i);
            list.add(iBool);
        }

        //发布部门
        List<JSONObject> listPublish = new ArrayList<>();
        if (!materialReq.getPublishDeptIds().isEmpty() && null !=materialReq.getPublishDeptIds()) {
            JSONObject uu = null;
            JSONObject m11 = null;
            JSONObject uu2 = null;
            JSONObject m12 = null;
            for (String deptId : materialReq.getPublishDeptIds()) {
                m11 = new JSONObject();
                m11.put(ElkUtils.ELK_IIR + ".iir_deptid" , deptId);
                uu = new JSONObject();
                uu.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, m11);
                listPublish.add(uu);
                m12 = new JSONObject();
                m12.put(ElkUtils.ELK_IIR + ".iir_parent_deptid" , deptId);
                uu2 = new JSONObject();
                uu2.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE, m12);
                listPublish.add(uu2);
            }
        }

        if(!listPublish.isEmpty()){
            JSONObject typeShoul = new JSONObject();
            typeShoul.put(ElkUtils.ELK_GRAMMAR_SHOULD,listPublish);
            JSONObject typeBool = new JSONObject();
            typeBool.put("bool",typeShoul);
            list.add(typeBool);
        }

        //上传者
        List<JSONObject> listPublishUser = new ArrayList<>();
        if (null != materialReq.getPublishUserIds() && !materialReq.getPublishUserIds().isEmpty()){
            JSONObject innerD = null;
            JSONObject outerD = null;
            for (String s : materialReq.getPublishUserIds()) {
                innerD = new JSONObject();
                outerD = new JSONObject();
                innerD.put(ElkUtils.ELK_IIR+".iir_publish_userid",s);
                outerD.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,innerD);
                listPublishUser.add(outerD);
            }
        }
        if(!listPublishUser.isEmpty()){
            JSONObject source = new JSONObject();
            source.put(ElkUtils.ELK_GRAMMAR_SHOULD,listPublishUser);
            JSONObject sourceBool = new JSONObject();
            sourceBool.put("bool",source);
            list.add(sourceBool);
        }

        //页数
        if(1==materialReq.getPages()){
            JSONObject inner = new JSONObject();
            inner.put(ElkUtils.ELK_GRAMMAR_GTE, 20);
            JSONObject out = new JSONObject();
            out.put("iir.iir_pages",inner);
            JSONObject rangePage = new JSONObject();
            rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE,out);
            list.add(rangePage);
        }else if(2==materialReq.getPages()){
            JSONObject inner = new JSONObject();
            inner.put(ElkUtils.ELK_GRAMMAR_GTE, 10);
            inner.put(ElkUtils.ELK_GRAMMAR_LTE,20);
            JSONObject out = new JSONObject();
            out.put("iir.iir_pages",inner);
            JSONObject rangePage = new JSONObject();
            rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE,out);
            list.add(rangePage);
        }else if(3==materialReq.getPages()){
            JSONObject inner = new JSONObject();
            inner.put(ElkUtils.ELK_GRAMMAR_LTE,10);
            JSONObject out = new JSONObject();
            out.put("iir.iir_pages",inner);
            JSONObject rangePage = new JSONObject();
            rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE,out);
            list.add(rangePage);
        }else if(4==materialReq.getPages()){
            JSONObject inner = new JSONObject();
            inner.put(ElkUtils.ELK_GRAMMAR_LTE,5);
            JSONObject out = new JSONObject();
            out.put("iir.iir_pages",inner);
            JSONObject rangePage = new JSONObject();
            rangePage.put(ElkUtils.ELK_GRAMMAR_RANGE,out);
            list.add(rangePage);
        }

        //当前用户是否有权限查看，添加用户id时要加上上传的id
        List<JSONObject> user = new ArrayList<>();
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        Long id = loginUser.getId();
        JSONObject u = new JSONObject();
        u.put("iir.iir_tag_id",id);
        JSONObject us = new JSONObject();
        us.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,u);
        user.add(us);
        JSONObject c = new JSONObject();
        c.put("iir.iir_user_id",loginUser.getId());
        JSONObject mc = new JSONObject();
        mc.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,c);
        JSONObject tag = new JSONObject();
        user.add(mc);
        tag.put("iir.iir_right_tag",1);
        JSONObject tm = new JSONObject();
        tm.put(ElkUtils.ELK_GRAMMAR_MATCH_PHRASE,tag);
        user.add(tm);
        JSONObject ss = new JSONObject();
        ss.put("should",user);
        JSONObject bb = new JSONObject();
        bb.put("bool",ss);
        list.add(bb);
        JSONObject queryMust = new JSONObject();
        queryMust.put("must",list);
        JSONObject queryBool = new JSONObject();
        queryBool.put("bool",queryMust);


        JSONObject params = new JSONObject();
        params.put("query",queryBool);
        params.put("track_total_hits",true);

        //分页实现
        Integer size = materialReq.getSize() == null || materialReq.getSize() == 0 ? 20 : materialReq.getSize();
        //排序
        List<JSONObject> listSort = new ArrayList<>();
        JSONObject publishTimeSort = new JSONObject();
        publishTimeSort.put(ElkUtils.ELK_IIR+".iir_currenttime",ElkUtils.ELK_GRAMMAR_DESC);
        listSort.add(publishTimeSort);
        JSONObject idSort = new JSONObject();
        //idSort.put("iir.iir_mysql_id","desc");
        // listSort.add(idSort);
        params.put(ElkUtils.ELK_GRAMMAR_SIZE,size);
        params.put(ElkUtils.ELK_GRAMMAR_SORT,listSort);

        //search_after
        if(!materialReq.getOrderNumber().isEmpty()){
            params.put(ElkUtils.ELK_GRAMMAR_SEARCH_AFTER,materialReq.getOrderNumber());
        }
        log.info("params:"+params);
        return params;
    }

}
